简体   繁体   中英

How to group the line in txt file to nested dictionary (Python)

I have a txt file that contain the data as below:

Id_1 1111
Member_a 2222
Member_b 3333
Member_c 4444
Device_a 5555
Device_b 6666

Id_2 1234
Member_a 5678
Member_b 1345
Device_a 3141

Id_3 1317
Member_a 5643
Member_b 4678
Member_c 4654
Member_e 4674
member_f 2314
Device_a 4433
Device_b 4354

.
.
.

and so on

There are 3 fields contained in each id and each field has a different number of sub-field (for example, some id has 4 members, but some have only 2 members) Is there any way that I could combine these data into something like a nested dictionary?

Here is the expected output:

{
 'id': 'Id_1', 
 'member': [{'Member_a': 2222}, {'Member_b': 3333}, {'Member_c': 4444}],
 'Device' : [{'Device_a': 5555}, {'Device_b': 6666}]
}

Thank you in advance!

In the code below, "blankpaper.txt" contains the text provided from you. I have constructed dictionaries that conform to the expected output and stored each one in the list dicts .

In my version, I have made all strings in the final output lower case though you can adjust the code accordingly based on your needs. I was not sure if your data file sometimes had for example, member_f , on one line, and Member_f on another line, or if that was just a typo above. If similar information is known, the code can be made more efficient.

dicts = []

with open("blankpaper.txt") as f:
    for line in f:
        # if line not empty
        if (split := line.split()):
            a = split[0].lower()
            b = a[0:a.rfind('_')]
            if "id" == b:
                temp = {'id': a, 'member': [], 'device': []}
                dicts.append(temp)
            elif "member" == b:
                temp['member'].append({a: int(split[1])})
            elif "device" == b:
                temp['device'].append({a: int(split[1])})

print(dicts)

Output

[{'id': 'id_1', 'member': [{'member_a': 2222}, {'member_b': 3333}, {'member_c': 4444}], 'device': [{'device_a': 5555}, {'device_b': 6666}]}, {'id': 'id_2', 'member': [{'member_a': 5678}, {'member_b': 1345}], 'device': [{'device_a': 3141}]}, {'id': 'id_3', 'member': [{'member_a': 5643}, {'member_b': 4678}, {'member_c': 4654}, {'member_e': 4674}, {'member_f': 2314}], 'device': [{'device_a': 4433}, {'device_b': 4354}]}]

I hope this helps. If there are any questions or if this is not exactly what you wanted, please let me know.

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