から辞書の辞書を作成し、第2列は、このような3番目の列の値にマッピングする所定のコードである:Pythonは - 私は多くの重複値を持つ最初の列とCSVを有するCSV
1, a, 24
1, b, 13
1, c, 30
1, d, 0
2, a, 1
2, b, 12
2, c, 82
2, d, 81
3, a, 04
3, b, 23
3, c, 74
3, d, 50
私は次のようになるだろうと、CSVからの辞書の辞書を作成しようとしています:
dict 1 = {'1':{'a':'24', 'b':'13', 'c':'30','d':'0'},
'2':{'a':'1', 'b':'12', 'c':'82','d':'81'},
... }
私のコードだけで罰金キー値を作成しますが、結果の値の辞書がすべて空である(ただし、いくつかの印刷ステートメントは、それらが実行中ではないことを示していますSS)...
with open(file, mode='rb') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
dict1 = {} # creates main dict
for row in reader: # iterates through the rows of the csvfile
if row[0] in dict1:
dict2[row[1]] = row[2] # adds another key, value to dict2
else:
dict1[row[0]] = {} # creates a new key entry for the new dict1 key
dict2 = {} # creates a new dict2 to start building as the value for the new dict1 key
dict2[row[1]] = row[2] # adds the first key, value pair for dict2
あなたは 'dict2'を' dict1'の一部として指定しませんでした。 – PatNowak