0
我試圖清理一個csv文件的內容,然後創建一個新的字典。我想新的字典,在全球範圍內提供:在循環兩次後創建新的字典
import csv
input_file = csv.DictReader(open("test_file3.csv"))
final_dict = {} #this should get filled with the new dictionary
for row in input_file: #cleaning the dictionary
new_dict = {}
for key, value in row.items():
if key == "Start Date":
new_dict[key] = value
else:
first_replace = value.replace(".", "")
second_replace = first_replace.replace(",", ".")
all_replaced = second_replace.replace(" €", "")
new_dict[key] = all_replaced
它的工作原理的第一循環中,但我不知道怎麼弄的字典下new_dict到final_dict
理想我想final_dict = new_dict。
你也可以在@dstromberg裏做final_dict.update(new_dict) – dstromberg
這就夠了,但是爲什麼當你可以訪問你想要填充的字典時創建一個新字典呢? – jonrsharpe
我現在有問題了,在final_dict中,我只有原始字典中的最後一個條目。我相信這是因爲用DictReader創建的原始字典包含csv中每行的字典。如何將原始csv中的所有字典合併到一個新字典中? – bigvic857