Ich lese JSON in mein Skript und erstelle eine Liste mit Wörterbüchern.Seltsames Verhalten beim Anhängen eines Wörterbuchs zur Liste
Meine JSON:
{
"JMF": {
"table1": {
"email": "[email protected]",
"guests": [
"test1",
"test2"
]
},
"table2": {
"email": "[email protected]",
"guests": [
"test3"
]
}
},
"JMC": {
"table3": {
"email": "[email protected]",
"guests": [
"test11"
]
}
},
"JMD": {
"table4": {
"email": "[email protected]",
"guests": [
"test12"
]
},
"table5": {
"email": "[email protected]",
"guests": [
"test17"
]
}
}
}
Mein Code:
def get_json():
userinfo_list = []
with open('guest_users.json') as json_file:
json_file = json.load(json_file)
keys = json_file.keys()
for key in keys:
userinfo = {}
for table_key in json_file[key].keys():
email = json_file[key][table_key]['email']
users_dict = {}
users_list = []
for user in json_file[key][table_key]['guests']:
users_dict['username'] = user
users_dict['password'] = generate_password()
users_list.append(users_dict)
userinfo['company'] = key
userinfo['email'] = email
userinfo['userinfo'] = users_list
userinfo_list.append(userinfo)
print(userinfo)
print(userinfo_list)
Das Problem ist, dass die Werte in userinfo_list
so schnell überschrieben werden wie meine JSON hat zwei Unterschlüssel (table*
).
Dies ist die Ausgabe erhalte ich, was keinen Sinn macht:
{'userinfo': [{'username': 'test11', 'password': '1fEAg0'}], 'email': '[email protected]', 'company': 'JMC'}
[{'userinfo': [{'username': 'test11', 'password': '1fEAg0'}], 'email': '[email protected]', 'company': 'JMC'}]
{'userinfo': [{'username': 'test17', 'password': 'A8Jue5'}], 'email': '[email protected]', 'company': 'JMD'}
[{'userinfo': [{'username': 'test11', 'password': '1fEAg0'}], 'email': '[email protected]', 'company': 'JMC'}, {'userinfo': [{'username': 'test17', 'password': 'A8Jue5'}], 'email': '[email protected]', 'company': 'JMD'}]
{'userinfo': [{'username': 'test12', 'password': '0JSpc0'}], 'email': '[email protected]', 'company': 'JMD'}
[{'userinfo': [{'username': 'test11', 'password': '1fEAg0'}], 'email': '[email protected]', 'company': 'JMC'}, {'userinfo': [{'username': 'test12', 'password': '0JSpc0'}], 'email': '[email protected]', 'company': 'JMD'}, {'userinfo': [{'username': 'test12', 'password': '0JSpc0'}], 'email': '[email protected]', 'company': 'JMD'}]
{'userinfo': [{'username': 'test2', 'password': 'GagQ59'}, {'username': 'test2', 'password': 'GagQ59'}], 'email': '[email protected]', 'company': 'JMF'}
[{'userinfo': [{'username': 'test11', 'password': '1fEAg0'}], 'email': '[email protected]', 'company': 'JMC'}, {'userinfo': [{'username': 'test12', 'password': '0JSpc0'}], 'email': '[email protected]', 'company': 'JMD'}, {'userinfo': [{'username': 'test12', 'password': '0JSpc0'}], 'email': '[email protected]', 'company': 'JMD'}, {'userinfo': [{'username': 'test2', 'password': 'GagQ59'}, {'username': 'test2', 'password': 'GagQ59'}], 'email': '[email protected]', 'company': 'JMF'}]
{'userinfo': [{'username': 'test3', 'password': 'U9gP0j'}], 'email': '[email protected]', 'company': 'JMF'}
[{'userinfo': [{'username': 'test11', 'password': '1fEAg0'}], 'email': '[email protected]', 'company': 'JMC'}, {'userinfo': [{'username': 'test12', 'password': '0JSpc0'}], 'email': '[email protected]', 'company': 'JMD'}, {'userinfo': [{'username': 'test12', 'password': '0JSpc0'}], 'email': '[email protected]', 'company': 'JMD'}, {'userinfo': [{'username': 'test3', 'password': 'U9gP0j'}], 'email': '[email protected]', 'company': 'JMF'}, {'userinfo': [{'username': 'test3', 'password': 'U9gP0j'}], 'email': '[email protected]', 'company': 'JMF'}]
Daten nicht überschreiben. Versuchen Sie immer, neue Listen, neue dicts, alles neue zu erstellen - wenn der RAM-Speicher es Ihnen erlaubt - und Sie werden es in 99% der Fälle erlauben. –