我正在尝试为作业创建一个特定的嵌套字典(别担心,我已经有权在这里发帖!),我遇到了一个我无法理解的奇怪错误。代码有点长,所以我只是发布我知道错误发生的区域(包括我用于故障排除的标签和显示错误的结果)。我已经粗略地说明了错误是什么。
csv_dict =所有项目的词典 被拉出来了。我正在展示一个迷你版 这是因为它很长(每个键20个值; 每个值中的第一个数字是给定的唯一ID 每个人:
{'地址':[{'17':'Ap#929-9420 Vivamus Rd。','16 ...... '姓名':[{'17':'Meredith F. Clayton','16'...... “电子邮件” ...
assignment_dict = {0 : 'Name', 1: 'Phone', 2: 'Address', 3 : 'City', 4: 'Country', 5: 'Email'}
csv_step1 = {}
csv_file = {'Name': [], 'Phone': [], 'Address': [], 'City': [], 'Country': [], 'Email': []}
counter = 0
while counter < 2:
list_of_dicts_to_convert = csv_dict[assignment_dict[counter]]
for csv_dict_value in list_of_dicts_to_convert:
for key, value in csv_dict_value.items():
csv_step1[key] = value
print(assignment_dict[counter], csv_step1)
csv_file[assignment_dict[counter]] = csv_step1
print(csv_file, "\n\n")
counter += 1
print("Final csv_file:", csv_file)
姓名''5':'Jescie Sargent','19':'Gabriel Richmond','7':'Bevis M. Santos','6':'凯西摩根','9':'查尔斯F Crawford','13':'Katelyn Munoz','11':'Elijah Myers','3':'Alexandra E. Saunders','16':'Tatyana H. French','14':'Genevieve Holland','17':'Meredith F. Clayton','15':'Wesley Z. Sharp','0':'Hillary Benton','2':'Camden Z. Blair','12':' Thane Burch','1':'Morgan Y. Little','8':'Flynn Alston','4':'Hanae P. Walsh','10':'Cairo Wolfe','18':'Rajah卡里略'}
当前的csv_file:{'地址':[],'姓名':{'5':'Jescie Sargent','19':'Gabriel Richmond','7':'Bevis M. Santos','6':'Kessie Morgan','9':'Charles F. Crawford','13':'Katelyn Munoz','11':'Elijah Myers','3':'Alexandra E Saunders','16':'Tatyana H. French','14':'Genevieve Holland','17':'Meredith F. Clayton','15':'Wesley Z. Sharp','0': 'Hillary Benton','2':'Camden Z. Blair','12':'Thane Burch','1':'Morgan Y. Little','8':'Flynn Alston','4':' Hanae P. Walsh','10':'Cairo Wolfe','18':'Rajah Carrillo'},'Country':[],'Phone':[],'Email':[],'City': []}
电话{'5':'265-1176','19':'1-387-932-2096','7':'227-9994','6':'945-0713',' 9':'791-5111','13':'220-5054','11':'1-238-336-4864','3':'1-637-740-7614','16' :'1-120-782-6047','14':'992-6968','17':'425-7583','15':'1-960-740-2261','0':' 1-243-669-7472','2':'123-5058','12':'1-894-978-3696','1':'155-3483','8':'398- 8097','4':'901-2461','10':'1-930-942-2322','18':'1-576-789-5730'}
当前的csv_file:{'地址':[],'姓名':{'5':'265-1176','19':'1-387-932-2096', '7':'227-9994','6':'945-0713','9':'791-5111','13':'220-5054','11':'1-238-336 -4864','3':'1-637-740-7614','16':'1-120-782-6047','14':'992-6968','17':'425-7583 ','15':'1-960-740-2261','0':'1-243-669-7472','2':'123-5058','12':'1-894-978 -3696','1':'155-3483','8':'398-8097','4':'901-2461','10':'1-930-942-2322','18 ':'1-576-789-5730'},'国家':[],'电话':{'5':'265-1176','19':'1-387 -932-2096','7':'227-9994','6':'945-0713','9':'791-5111','13':'220-5054','11': '1-238-336-4864','3':'1-637-740-7614','16':'1-120-782-6047','14':'992-6968','17 ':'425-7583','15':'1-960-740-2261','0':'1-243-669-7472','2':'123-5058','12': '1-894-978-3696','1':'155-3483','8':'398-8097','4':'901-2461','10':'1-930-942 -2322','18':'1-576-789-5730'},'电子邮件':[],'城市':[]}
最终csv_file:{'地址':[],'姓名':{'5':'265-1176','19':'1-387-932-2096', '7':'227-9994','6':'945-0713','9':'791-5111','13':'220-5054','11':'1-238-336 -4864','3':'1-637-740-7614','16':'1-120-782-6047','14':'992-6968','17':'425-7583 ','15':'1-960-740-2261','0':'1-243-669-7472','2':'123-5058','12':'1-894-978 -3696','1':'155-3483','8':'398-8097','4':'901-2461','10':'1-930-942-2322','18 ':'1-576-789-5730'},'国家':[],'电话':{'5':'265-1176','19':'1-387 -932-2096','7':'227-9994','6':'945-0713','9':'791-5111','13':'220-5054','11': '1-238-336-4864','3':'1-637-740-7614','16':'1-120-782-6047','14':'992-6968','17 ':'425-7583','15':'1-960-740-2261','0':'1-243-669-7472','2':'123-5058','12': '1-894-978-3696','1':'155-3483','8':'398-8097','4':'901-2461','10':'1-930-942 -2322','18':'1-576-789-5730'},'电子邮件':[],'城市':[]}
由于某种原因,电话号码正在替换名称部分中的名称,即使循环只应填写csv_file [assignment_dict [counter]]当前值的键值。如果我进入完整循环(填充电子邮件,城市,国家和地址),电子邮件最终将最终替换所有这些(因为它是循环中的最后一个)。
我试图阻止这一点,但到目前为止我尝试过的所有事情都失败了。有关如何正确填充这个的想法吗?感谢
答案 0 :(得分:0)
在不知道什么是csv_dict以及什么是assignment_dict的情况下难以猜测。这是一个猜测:
你是否在while循环中缺少csv_step1 = {}初始化?