Russo Russo - 1 month ago 9
Python Question

Creating List of List from variable and Print the final list after each For Loop - Python

I have a log file that looks like this:

|Label: - Name:locale - Type:
|Label:Main Menu - Name:menu - Type:
||Label:Broadband - Name:menu - Type:
|||Label:Load and Save Profiles - Name:menu - Type:
||||Label:Load Profile - Name:application - Type:
||||Label:Save Profile - Name:application - Type:
||||Label:Delete Profile - Name:application - Type:
|||Label:Interface - Name:parameter - Type:list
||||Label:xDSL - Name:value - Type:
||||Label:SFP - Name:value - Type:
||||Label:Ethernet - Name:value - Type:
||||Label:SHDSL - Name:value - Type:
|||Label:xDSL Interface - Name:menu - Type:
||||Label:xDSL Mode - Name:parameter - Type:list
|||||Label:Annex A/M - Name:value - Type:
|||||Label:Annex B/J - Name:value - Type:
||||Label:MAC Address - Name:application - Type:
|||||Label:MAC Address - Name:param - Type:string
||||Label:Vectoring Mode - Name:parameter - Type:list
|||||Label:Disabled - Name:value - Type:
|||||Label:Enabled - Name:value - Type:
|||||Label:Friendly - Name:value - Type:
||||Label:G.FAST - Name:parameter - Type:list
|||||Label:Disabled - Name:value - Type:
|||||Label:Enabled - Name:value - Type:
||||Label:Auto Re-Sync - Name:parameter - Type:list
|||||Label:On - Name:value - Type:
|||||Label:Off - Name:value - Type:


I want to write a python code that will read the txt file and create a list of lists and print the list file after each for loop.

This is the code I have written:

dict = {}

with open("/home/mr/Desktop/git/Squish/parsedXML.txt") as f:

for line in f:
if "list" in line:
list_title = re.search('Label:(.*) - N', line).group(1)
dict['lst_%s' % list_title] = []

if "value" in line:
list_option = re.search('Label:(.*) - N', line).group(1)
dict['lst_%s' % list_title].append(list_option)
test.log(str(dict)) #The print command for the IDE I am using


The output of the code is:

TestCase tst_xml_tree_2 Aug 28, 2017 11:57:37 AM
Log {'lst_Interface': ['xDSL']} Aug 28, 2017 11:57:37 AM
Log {'lst_Interface': ['xDSL', 'SFP']} Aug 28, 2017 11:57:37 AM
Log {'lst_Interface': ['xDSL', 'SFP', 'Ethernet']} Aug 28, 2017 11:57:37 AM
Log {'lst_Interface': ['xDSL', 'SFP', 'Ethernet', 'SHDSL']} Aug 28, 2017 11:57:37 AM
Log {'lst_Interface': ['xDSL', 'SFP', 'Ethernet', 'SHDSL'], 'lst_xDSL Mode': ['Annex A/M']} Aug 28, 2017 11:57:37 AM
Log {'lst_Interface': ['xDSL', 'SFP', 'Ethernet', 'SHDSL'], 'lst_xDSL Mode': ['Annex A/M', 'Annex B/J']} Aug 28, 2017 11:57:37 AM
Log {'lst_Interface': ['xDSL', 'SFP', 'Ethernet', 'SHDSL'], 'lst_Vectoring Mode': ['Disabled'], 'lst_xDSL Mode': ['Annex A/M', 'Annex B/J']} Aug 28, 2017 11:57:37 AM
Log {'lst_Interface': ['xDSL', 'SFP', 'Ethernet', 'SHDSL'], 'lst_Vectoring Mode': ['Disabled', 'Enabled'], 'lst_xDSL Mode': ['Annex A/M', 'Annex B/J']} Aug 28, 2017 11:57:37 AM
Log {'lst_Interface': ['xDSL', 'SFP', 'Ethernet', 'SHDSL'], 'lst_Vectoring Mode': ['Disabled', 'Enabled', 'Friendly'], 'lst_xDSL Mode': ['Annex A/M', 'Annex B/J']} Aug 28, 2017 11:57:37 AM
Log {'lst_Interface': ['xDSL', 'SFP', 'Ethernet', 'SHDSL'], 'lst_Vectoring Mode': ['Disabled', 'Enabled', 'Friendly'], 'lst_xDSL Mode': ['Annex A/M', 'Annex B/J'], 'lst_G.FAST': ['Disabled']} Aug 28, 2017 11:57:37 AM
Log {'lst_Interface': ['xDSL', 'SFP', 'Ethernet', 'SHDSL'], 'lst_Vectoring Mode': ['Disabled', 'Enabled', 'Friendly'], 'lst_xDSL Mode': ['Annex A/M', 'Annex B/J'], 'lst_G.FAST': ['Disabled', 'Enabled']} Aug 28, 2017 11:57:37 AM
Log {'lst_Interface': ['xDSL', 'SFP', 'Ethernet', 'SHDSL'], 'lst_Vectoring Mode': ['Disabled', 'Enabled', 'Friendly'], 'lst_xDSL Mode': ['Annex A/M', 'Annex B/J'], 'lst_Auto Re-Sync': ['On'], 'lst_G.FAST': ['Disabled', 'Enabled']} Aug 28, 2017 11:57:37 AM
Log {'lst_Interface': ['xDSL', 'SFP', 'Ethernet', 'SHDSL'], 'lst_Vectoring Mode': ['Disabled', 'Enabled', 'Friendly'], 'lst_xDSL Mode': ['Annex A/M', 'Annex B/J'], 'lst_Auto Re-Sync': ['On', 'Off'], 'lst_G.FAST': ['Disabled', 'Enabled']} Aug 28, 2017 11:57:37 AM


But if you look at the output, it prints everytime it appends a string in the list. I want to print the final output of the list after every for loop. Like this:

Log {'lst_Interface': ['xDSL', 'SFP', 'Ethernet', 'SHDSL']} Aug 28, 2017 11:57:37
Log {'lst_Interface': ['xDSL', 'SFP', 'Ethernet', 'SHDSL'], 'lst_xDSL Mode': ['Annex A/M', 'Annex B/J']} Aug 28, 2017 11:57:37 AM
Log {'lst_Interface': ['xDSL', 'SFP', 'Ethernet', 'SHDSL'], 'lst_Vectoring Mode': ['Disabled', 'Enabled', 'Friendly'], 'lst_xDSL Mode': ['Annex A/M', 'Annex B/J']} Aug 28, 2017 11:57:37 AM
Log {'lst_Interface': ['xDSL', 'SFP', 'Ethernet', 'SHDSL'], 'lst_Vectoring Mode': ['Disabled', 'Enabled', 'Friendly'], 'lst_xDSL Mode': ['Annex A/M', 'Annex B/J'], 'lst_Auto Re-Sync': ['On', 'Off'], 'lst_G.FAST': ['Disabled', 'Enabled']} Aug 28, 2017 11:57:37 AM


Can anyone please tell me how should I achieve this?

If I put the print command outside of the loop like this:

dict = {}

with open("/home/mr/Desktop/git/Squish/parsedXML.txt") as f:

for line in f:
if "list" in line:
list_title = re.search('Label:(.*) - N', line).group(1)
dict['lst_%s' % list_title] = []

if "value" in line:
list_option = re.search('Label:(.*) - N', line).group(1)
dict['lst_%s' % list_title].append(list_option)
test.log(str(dict)) #The print command for the IDE I am using


It prints the following output:

TestCase tst_asd Aug 28, 2017 12:26:06 PM
Log {} Aug 28, 2017 12:26:06 PM
Log {} Aug 28, 2017 12:26:06 PM
Log {} Aug 28, 2017 12:26:06 PM
Log {} Aug 28, 2017 12:26:06 PM
Log {} Aug 28, 2017 12:26:06 PM
Log {} Aug 28, 2017 12:26:06 PM
Log {} Aug 28, 2017 12:26:06 PM
Log {'lst_Interface': []} Aug 28, 2017 12:26:06 PM
Log {'lst_Interface': ['xDSL']} Aug 28, 2017 12:26:06 PM
Log {'lst_Interface': ['xDSL', 'SFP']} Aug 28, 2017 12:26:06 PM
Log {'lst_Interface': ['xDSL', 'SFP', 'Ethernet']} Aug 28, 2017 12:26:06 PM
Log {'lst_Interface': ['xDSL', 'SFP', 'Ethernet', 'SHDSL']} Aug 28, 2017 12:26:06 PM
Log {'lst_Interface': ['xDSL', 'SFP', 'Ethernet', 'SHDSL']} Aug 28, 2017 12:26:06 PM
Log {'lst_Interface': ['xDSL', 'SFP', 'Ethernet', 'SHDSL'], 'lst_xDSL Mode': []} Aug 28, 2017 12:26:06 PM
Log {'lst_Interface': ['xDSL', 'SFP', 'Ethernet', 'SHDSL'], 'lst_xDSL Mode': ['Annex A/M']} Aug 28, 2017 12:26:06 PM
Log {'lst_Interface': ['xDSL', 'SFP', 'Ethernet', 'SHDSL'], 'lst_xDSL Mode': ['Annex A/M', 'Annex B/J']} Aug 28, 2017 12:26:06 PM
Log {'lst_Interface': ['xDSL', 'SFP', 'Ethernet', 'SHDSL'], 'lst_xDSL Mode': ['Annex A/M', 'Annex B/J']} Aug 28, 2017 12:26:06 PM
Log {'lst_Interface': ['xDSL', 'SFP', 'Ethernet', 'SHDSL'], 'lst_xDSL Mode': ['Annex A/M', 'Annex B/J']} Aug 28, 2017 12:26:06 PM
Log {'lst_Interface': ['xDSL', 'SFP', 'Ethernet', 'SHDSL'], 'lst_Vectoring Mode': [], 'lst_xDSL Mode': ['Annex A/M', 'Annex B/J']} Aug 28, 2017 12:26:06 PM
Log {'lst_Interface': ['xDSL', 'SFP', 'Ethernet', 'SHDSL'], 'lst_Vectoring Mode': ['Disabled'], 'lst_xDSL Mode': ['Annex A/M', 'Annex B/J']} Aug 28, 2017 12:26:06 PM
Log {'lst_Interface': ['xDSL', 'SFP', 'Ethernet', 'SHDSL'], 'lst_Vectoring Mode': ['Disabled', 'Enabled'], 'lst_xDSL Mode': ['Annex A/M', 'Annex B/J']} Aug 28, 2017 12:26:06 PM
Log {'lst_Interface': ['xDSL', 'SFP', 'Ethernet', 'SHDSL'], 'lst_Vectoring Mode': ['Disabled', 'Enabled', 'Friendly'], 'lst_xDSL Mode': ['Annex A/M', 'Annex B/J']} Aug 28, 2017 12:26:06 PM
Log {'lst_Interface': ['xDSL', 'SFP', 'Ethernet', 'SHDSL'], 'lst_Vectoring Mode': ['Disabled', 'Enabled', 'Friendly'], 'lst_xDSL Mode': ['Annex A/M', 'Annex B/J'], 'lst_G.FAST': []} Aug 28, 2017 12:26:06 PM


Also another issue is, when a new list is created inside that list, here in my code it should be created in this sequence:

'lst_Interface' > 'lst_xDSL Mode' > 'lst_Vectoring Mode' > 'lst_G.FAST' > 'lst_Auto Re-Sync'


But if you look at my final line of the log, you will see that the lists sequence is:

'lst_Interface' > 'lst_Vectoring Mode' > 'lst_xDSL Mode' > 'lst_Auto Re-Sync' > 'lst_G.FAST'


How can I maintain the sequence of the lists?

Further clarification of the question:

I am actually going through a XML file of an application and I want to create a new list every time the string "list" is found on the XML, and immediately after that will be a set of "values" which I want to append in that list and then print the final list. And then when it's done, it will continue looking into the file and when it finds another "list" string in the XML file, it will create another list and so on. Isn't there any way to achieve that?

Bit Bit
Answer Source

Let me see if I understood this correctly: you want to execute a print statement only after a group of 'values' is parsed. Here is my take:

import re
from collections import OrderedDict

history = OrderedDict()

with open("file.txt") as f:
    flag = False
    for line in f:
        if "value" in line:
            list_option = re.search('Label:(.*) - N', line).group(1)
            history['lst_%s' % list_title].append(list_option)
            flag = True
        elif flag:  # executes only if previous line was a 'value' and this one is not.
            print(str(history))  # Replace this print statement with yours.
            flag = False
        if "list" in line:
            list_title = re.search('Label:(.*) - N', line).group(1)
            history.update({'lst_%s' % list_title: []})
    else:
        print(str(history))  # Replace this one as well.

Which produces:

OrderedDict([('lst_Interface', ['xDSL', 'SFP', 'Ethernet', 'SHDSL'])])
OrderedDict([('lst_Interface', ['xDSL', 'SFP', 'Ethernet', 'SHDSL']), ('lst_xDSL Mode', ['Annex A/M', 'Annex B/J'])])
OrderedDict([('lst_Interface', ['xDSL', 'SFP', 'Ethernet', 'SHDSL']), ('lst_xDSL Mode', ['Annex A/M', 'Annex B/J']), ('lst_Vectoring Mode', ['Disabled', 'Enabled', 'Friendly'])])
OrderedDict([('lst_Interface', ['xDSL', 'SFP', 'Ethernet', 'SHDSL']), ('lst_xDSL Mode', ['Annex A/M', 'Annex B/J']), ('lst_Vectoring Mode', ['Disabled', 'Enabled', 'Friendly']), ('lst_G.FAST', ['Disabled', 'Enabled'])])
OrderedDict([('lst_Interface', ['xDSL', 'SFP', 'Ethernet', 'SHDSL']), ('lst_xDSL Mode', ['Annex A/M', 'Annex B/J']), ('lst_Vectoring Mode', ['Disabled', 'Enabled', 'Friendly']), ('lst_G.FAST', ['Disabled', 'Enabled']), ('lst_Auto Re-Sync', ['On', 'Off'])])

I used OrderedDict since you need to preserve insertion order.