我正在將JSON讀入我的腳本並構建一個由字典組成的列表。將字典附加到列表中時出現奇怪行爲
我的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"
]
}
}
}
我的代碼:
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)
的問題是,我的JSON有兩個子鍵(table*
)在userinfo_list
價值得到儘快覆蓋。
這是輸出我得到的,這沒有任何意義:
{'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'}]
不要覆蓋數據。總是嘗試創建新的列表,新的字典,新的一切 - 如果RAM內存允許你 - 並且它可以讓你在99%的情況下。 –