1

我有一个包含以下字符串的变量:

fruit_wanted = 'banana,apple'

我也有一个 csv 文件

fruit,'orange','grape','banana','mango','apple','strawberry'
number,1,2,3,4,5,6
value,3,2,2,4,2,1
price,3,2,1,2,3,4

现在如何删除“fruit_wanted”变量中未列出“fruit”的列?

所以outfile看起来像

fruit,'banana','apple'
number,3,5
value,2,2
price,1,3

谢谢你。

4

2 回答 2

7

DictReader()使用class读取 csv 文件,并忽略不需要的列:

fruit_wanted = ['fruit'] + ["'%s'" % f for f in fruit_wanted.split(',')]
outfile = csv.DictWriter(open(outputfile, 'wb'), fieldnames=fruit_wanted)
fruit_wanted = set(fruit_wanted)

for row in csv.DictReader(open(inputfile, 'rb')):
    row = {k: row[k] for k in row if k in fruit_wanted}
    outfile.writerow(row)
于 2012-11-28T21:43:35.790 回答
0

这是一些伪代码:

open the original CSV for input, and the new one for output
read the first row of the original CSV and figure out which columns you want to delete
write the modified first row to the output CSV
for each row in the input CSV:
    delete the columns you figured out before
    write the modified row to the output CSV
于 2012-11-28T21:43:53.650 回答