如何创建新的连锁列?

时间:2012-12-05 16:55:13

标签: python

我有一个.csv,其数据如下:

30,972.74176,0.446378,1.825426,cl+l  
31,1015.075579,0.279686,2.950118,l  
32,1072.59334,0.273239,4.408458,cl+l  
33,1139.55317,-1.616904,4.756378,l  
34,1279.86304,50.335186,3.877577,r  
35,1181.541419,61.399389,5.155133,r  
36,1097.421199,63.809733,5.37508,r  
37,1018.555678,64.034432,2.59232,r

我编写了一个程序来取出最后一列(E,我在程序中调用它)的行是“l”或“c​​l + l”并将其复制到另一个名为“left”的文件中.csv”文件。我想要做的是在left.csv中添加另一列,从第二列(B)获取数据,在它们之间加上逗号,然后是D列中的数据。

所以,left.csv现在看起来像:

30,972.74176,0.446378,1.825426,cl+l  
31,1015.075579,0.279686,2.950118,l  
32,1072.59334,0.273239,4.408458,cl+l  
33,1139.55317,-1.616904,4.756378,l

我想要的是:

30,972.74176,0.446378,1.825426,cl+l,"972.74176,1.825426"  
31,1015.075579,0.279686,2.950118,l,"1015.075579,2.950118"    
32,1072.59334,0.273239,4.408458,cl+l,"1072.59334,4.408458"   
33,1139.55317,-1.616904,4.756378,l,"1139.55317,4.756378"

我应该如何实现这一点,如何告诉程序即使它是csv,逗号也不应该开始下一个条目?这是可能的,我是否必须有括号或不同的分隔符?我现在的代码是:

import csv
import sys
import operator

fieldnames = ["A","B","C","D","E"]
surveyfile = open("source.csv", "r")
left_file = open("left.csv",'wb')
right_file = open("right.csv",'wb')
coil_file = open("coil1.csv","wb")


reader = csv.DictReader(surveyfile, fieldnames=fieldnames, delimiter=",")
left_writer = csv.DictWriter(left_file, fieldnames, delimiter=",")
sortedlefts = sorted(reader,key=lambda x:float(x["B"]))

surveyfile.seek(0,0)

right_writer = csv.DictWriter(right_file, fieldnames, delimiter=",")
sortedrights = sorted(reader,key=lambda x:float(x["B"]), reverse=True)

coil_writer = csv.DictWriter(coil_file, fieldnames, delimiter=",")

for row in sortedlefts:
    if row["E"] == "l" or row["E"] == "cl+l":
        left_writer.writerow(row)
        coil_writer.writerow(row)   


for row in sortedrights:
    if row["E"] == "r":
        right_writer.writerow(row)
        coil_writer.writerow(row)

1 个答案:

答案 0 :(得分:3)

只需向row添加一个额外值,格式化为包含逗号:

row['F'] = '%s,%s' % (row['E'], row['B'])

或使用','.join()

row['F'] = ','.join([row['E'], row['B']])

但请在'F'的字段中添加left_writer

left_writer = csv.DictWriter(left_file, fieldnames + ['F'], delimiter=",")

如果您将coil_writer实例配置为忽略额外列,则无需为其创建单独的循环,也无需在现有列中创建row的副本循环:

coil_writer = csv.DictWriter(coil_file, fieldnames, delimiter=",", extrasaction='ignore')

csv作家将为您处理报价。