現在,當我需要所有行時,data和filenameVariable會打印最後一行。我嘗試過.append,但沒有奏效。我還能用什麼?字典在Python中追加
這是我的工作數據:
someCSVfile.csv|cust_no,0|streetaddr,1|city,2|state,3|zip,4|phone_home,5|firstname,6|lastname,7|status,9|
someCSVfile1.csv|cust_no,0|streetaddr,1|city,2|state,3|zip,4|phone_home,5|firstname,6|lastname,7|status,9|
下面是到目前爲止的代碼:
import csv
reader = csv.reader(open('match_log.txt','rb'), dialect='excel', delimiter='|')
data = {}
for row in reader:
filenameVariable = row[0]
data = dict(item.split(',') for item in row[1:])
print data
print filenameVariable
#right now its taking the final row. I need all rows
輸出什麼數據結構,你找誰?字典列表?字典詞典(關鍵是什麼)?目前,您在每次迭代時都會創建一個新字典,但不要對它做任何事情。 – jonrsharpe
**如何** .append()不起作用?你在這裏每循環迭代一次用一個新字典替換'data';你有沒有嘗試創建一個列表並附加到該列表? –