我正在根據第32列(它包含不同的節點名稱)從單個csv文件(data_labelled.csv) 寫入不同的csv文件,我希望每個節點都有一個單獨的文件。csv寫入錯誤
CSV文件:
2040696 58.475039683 4.6011785714 4.9225238095 1.3880952381 550.2218254 968424.34127 1046524 job_1456818565071_0001 task_1456818565071_0001_r_000000 REDUCE aditi 0
2040692 56.9 5.89 6.12 2 543.66666667 1045940 1046524 job_1456990723980_0001 task_1456990723980_0001_r_000000 REDUCE kalpesh 0
2040692 56.9 4.5633333333 5.2066666667 3 544 1045940 1046524 job_1456990723980_0001 task_1456990723980_0001_r_000000 REDUCE kalpesh 0
2040692 56.9 3.9 4.75 3 544 1045940 1046524 job_1456990723980_0001 task_1456990723980_0001_r_000000 REDUCE kalpesh 0
2040696 57.1 8.63 10.356666667 19.666666667 573 1046524 1046524 job_1456990723980_0001 task_1456990723980_0001_m_000000 MAP pooja 0
2040696 57.1 11.82 14.55 29 626 1046524 1046524 job_1456990723980_0001 task_1456990723980_0001_m_000000 MAP pooja 0
2040696 57.1 11.82 14.55 29 626 1046524 1046524 job_1456990723980_0001 task_1456990723980_0001_m_000000 MAP pooja 0
按代碼應該是具有三個不同的文件pooja.csv,kalpesh.csv,aditi.csv但只有kalpesh.csv正確完成其他兩個是空的。
代碼:
file = csv.reader(open('data_labelled.csv','r'))
print node_list
for node in node_list:
filename = '{}.csv'.format(node)
file2_obj=open(filename,'w')
file2 = csv.writer(file2_obj)
entries=list(file)
for entry in entries:
if(entry[32] == node):
file2.writerow(entry)
file2_obj.close()
結果爲node_list好:
[u'kalpesh', u'aditi', u'pooja']
,但是爲什麼其他兩個文件是空的? 怎麼回事?
當您執行'entries = list(file)'時,它會耗盡數據,以便下次獲取空列表時。查看下面的答案,只是簡單地將該語句放在循環之外。 – innoSPG