简体   繁体   中英

Text extraction into list using Python

I'm trying to parse a text file with content as below using Python code.

class: A1                
Name: anoop
Name: raj
Name: vish01
class: A2
Name: Billy
Name: bob
Name: Rick
Name: robert

And trying to parse this information in to a dictionary which would look like:

{A1:[anoop,raj,vish01],A2:[Billy,bob,Rick,robert]}

I wrote a piece of code but it only updates last name from the iteration, couldn't get the logic to implement the iteration.

dict1 = {}
with open("test_ldap.txt") as fh:
    for line in fh:
        if "class" in line:
            mlist = line.split()[1]
        if "name" in line:       
            s = str(line.split()[1])
            mylist = []
            mylist.append(s)
            dict1.update({mlist: mylist})
print(dict1)

My output

{'A1': ['vish01'], 'A2': ['robert']}

Every time you encounter a 'name' line, you create a new list and set it as the value for the name of the class. You should instead create the list when you get a 'class' line, and append to the same list for each subsequent 'name' line.

for line in fh:
  if line.startswith('class'):
    mlist = line.split()[1]
    dict1[mlist] = []
  elif line.startswith('name'):
    name = line.split()[1]
    dict1[mlist].append(name)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM