Python Forum
Converting Raw output into Nested List
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting Raw output into Nested List
#11
I'm not getting that with my modified version of your code. I'm getting what (I think) is the correct output:

Output:
[['vlan 1'], ['vlan 2', ' name UNUSED', ' mode vpc'], ['vlan 10', ' name SALES', ' mode vpc']] [['vlan 1'], ['vlan 2', ' name UNUSED', ' mode vpc'], ['vlan 10', ' name HR', ' mode vpc']]
Please repost your current code so I can compare it to my version of your code.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#12
    import re
    data_cleaned = {}
    current_key = ''
    action_flag = False
    data_group = []
    if_found_vlan = True

    output = open('./file1.txt','r').read()
    

    switch_red = re.findall(r'(\w*-RED\d{0,1})', output)[0]
    switch_blue = re.findall(r'(\w*-BLUE\d{0,1})', output)[0]

    for line in open('./file1.txt'):
        
        m = re.search(r'(\w*-RED\d{0,1}|\w*-BLUE\d{0,1})# sh run vlan \d+', line)
        #print m
        if m:
            if not if_found_vlan:
                data_cleaned[current_key].append([])
                print current_key
            if_found_vlan = False

            current_key = m.group(1)
            if not data_cleaned.has_key(current_key):
                data_cleaned[current_key] = []
            continue
        
        mm = re.match(r'vlan \d+', line)
        if mm:
            if_found_vlan = True
            action_flag = True
            data_group = []
        if action_flag:
            #action_flag = False
            data_cleaned[current_key].append(data_group)

        if action_flag:
            data_group.append(line.replace('\r', '').replace('\n', ''))

    if not if_found_vlan:
        data_cleaned[current_key].append([])
    print data_cleaned[switch_red]
    print "************************************"
    print data_cleaned[switch_blue]
Reply
#13
Well, you didn't put in that last section of code I mentioned. My vlan matching part looks like this:

    mm = re.match(r'vlan \d+', line)
    if mm:
        if action_flag:
            data_cleaned[current_key].append(data_group)
        if_found_vlan = True
        action_flag = True
        data_group = []
    if action_flag and '' == line.strip():
        action_flag = False
        data_cleaned[current_key].append(data_group)
Notice lines 3 and 4. And, as noted, it seems to be giving the correct output.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#14
@ichabod801 it worked..Thanks a lot.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List all possibilities of a nested-list by flattened lists sparkt 1 989 Feb-23-2023, 02:21 PM
Last Post: sparkt
  sum() list from SQLAlchemy output Personne 5 4,685 May-17-2022, 12:25 AM
Last Post: Personne
  Updating nested dict list keys tbaror 2 1,340 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Python Program to Find the Total Sum of a Nested List vlearner 8 5,147 Jan-23-2022, 07:20 PM
Last Post: menator01
  Looping through nested elements and updating the original list Alex_James 3 2,212 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  Converting a list to dictinary tester_V 8 2,830 Jul-02-2021, 09:04 PM
Last Post: tester_V
  format the output from a nested dictionary. nostradamus64 9 4,795 May-03-2021, 04:45 PM
Last Post: nostradamus64
  Converting tkinter listbox into list BigSwiggy 6 3,772 Feb-07-2021, 02:01 PM
Last Post: BigSwiggy
  shuffle a nested list giorgosmarga 11 12,136 Nov-12-2020, 07:04 PM
Last Post: perfringo
Question Save list with nested list into CSV SpongeB0B 1 5,536 Oct-12-2020, 07:26 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020