0

我有一个 .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)
4

1 回答 1

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 编写器将为您处理报价。

于 2012-12-05T16:59:05.153 回答