Home > Mobile >  Dictionaries inside dictionary not working
Dictionaries inside dictionary not working

Time:11-05

with open('personnages.txt') as file:
my_file = file.read()
f = my_file.split('\n\n')
final_dict = {}

for row1 in f:
    data = row1.strip().split('\n')
    d = {}
    for row2 in data:
        key, value = row2.split(':')
        d[key] = value
        for row3 in d:
            name_dict = {}
            final_dict[d['nom']] = name_dict
            name_dict['genre'] = d['genre']
            name_dict['accessoires'] = d['accessoires']
            name_dict['cheveux'] = d['cheveux']
            name_dict['yeux'] = d['yeux']
            name_dict['nez'] = d['nez']
            name_dict['pilosite'] = d['pilosite']

If I print final_dict likeso:

with open('personnages.txt') as file:
my_file = file.read()
f = my_file.split('\n\n')
final_dict = {}

for row1 in f:
    data = row1.strip().split('\n')
    d = {}
    for row2 in data:
        key, value = row2.split(':')
        d[key] = value
        for row3 in d:
            name_dict = {}
            final_dict[d['nom']] = name_dict
    print(final_dict)

it gives

{'Alex': {}, 'Alfred': {}, 'Anita': {}, 'Anne': {}, 'Bernard': {}, 'Bill': {}, 'Charles': {}, 'Claire': {}, 'David': {}, 'Eric': {}, 'Frans': {}, 'George': {}, 'Herman': {}, 'Joe': {}, 'Maria': {}, 'Max': {}, 'Paul': {}, 'Peter': {}, 'Philip': {}, 'Richard': {}, 'Robert': {}, 'Sam': {}, 'Susan': {}, 'Tom': {}}

Each bracket {} contains a dictionary where the key is genre, accessoires and value whats on the right. I dont know why its not going beetween the {}.

I'm looking to have this in the output :

{'Alex': {'genre': 'homme', 'nez': 'petit', 'yeux': 'bruns'},
 'Alfred': {'genre': 'homme', 'nez': 'gros', 'yeux': 'bleus'},
 'Anita': {'genre': 'femme', 'nez': 'petit', 'yeux': 'bleus'},
 'Anne': {'genre': 'femme', 'nez': 'gros', 'yeux': 'bruns'},
 'Bernard': {'genre': 'homme', 'nez': 'gros', 'yeux': 'bruns'},
 'Bill': {'genre': 'homme', 'nez': 'gros', 'yeux': 'bruns'}}

CodePudding user response:

This ...

        for row3 in d:
            name_dict = {}
            final_dict[d['nom']] = name_dict

... maps an empty dict to key d['nom'] in dict final_dict. Multiple times. Instead, you want to either

  1. Collect all the the data for one 'nom' into a dictionary first, then add that dictionary, OR

  2. Add a dictionary for a given 'nom', then add entries to that dictionary as they are discovered.

If you cannot rely on the 'nom' entry to be the first read from the file then the former is your best bet. It might look like this:

final_dict = {}

for row1 in f:
    data = row1.strip().split('\n')
    d = {}
    for row2 in data:
        key, value = row2.split(':')
        d[key] = value

    # The whole line has now been parsed
    final_dict[d['nom']] = d

    # Remove the 'nom' entry from the inner dict
    del d['nom']

# print the result (once for all)
print(final_dict)

CodePudding user response:

nested_dict = { 'dictA': {'key_1': 'value_1'}, 'dictB': {'key_2': 'value_2'}}

  • Related