我正在从单个csv文件中编写不同的csv文件(data_labelled.csv) 基于第32列(它包含不同的节点名称),我希望每个节点都有一个单独的文件。
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']
但是为什么其他两个文件都是空的? 出了什么问题?
答案 0 :(得分:1)
将entries=list(file)
放出循环,这就是问题所在。
您的代码应如下所示。
file = csv.reader(open('data_labelled.csv','r'))
entries=list(file)
print node_list
for node in node_list:
filename = '{}.csv'.format(node)
file2_obj=open(filename,'w')
file2 = csv.writer(file2_obj)
for entry in entries:
if(entry[32] == node):
file2.writerow(entry)
file2_obj.close()