输入文件:

$ cat test.csv
company,spread,cat1,cat2,cat3
A,XYZ,32,67,0
B,XYZ,43,0,432
C,XYZ,32,76,32
D,XYZ,454,87,43
E,XYZ,0,0,65
F,XYZ,0,0,7

预期的csv输出(sum列cat1cat2cat3并附加sum):
$ cat test.csv
company,spread,cat1,cat2,cat3
A,XYZ,32,67,0
B,XYZ,43,0,432
C,XYZ,32,76,32
D,XYZ,454,87,43
E,XYZ,0,0,65
F,XYZ,0,0,7
,,561,230,579

代码:
import csv

all_keys = ['cat1', 'cat2', 'cat3']
default_values = {i: 0 for i in all_keys}

def read_csv():
    with open('test.csv', 'r') as f:
        reader = csv.DictReader(f)
        yield from reader

for row in read_csv():
    for i in all_keys:
        default_values[i] += int(row[i])

with open('test.csv', 'a') as w:
    writer = csv.DictWriter(w, fieldnames=all_keys)
    writer.writerow(default_values)

实际产量:
$ cat test.csv
company,spread,cat1,cat2,cat3
A,XYZ,32,67,0
B,XYZ,43,0,432
C,XYZ,32,76,32
D,XYZ,454,87,43
E,XYZ,0,0,65
F,XYZ,0,0,7
561,230,579

问题:
csv.DictWriter没有以正确的列对齐方式追加行。我知道我有5列,但我只提供3列的值。但我认为,由于这是DictWriter,它将只向匹配的列标题追加值。如果我打开Actual Outputcsv,很明显列没有对齐:
python - CSV行附加:DictWriter始终从第一列写入-LMLPHP

最佳答案

您应该在fieldnames中包含前两个的列名:

with open('test.csv', 'a') as w:
    writer = csv.DictWriter(w, fieldnames=['company', 'spread']+all_keys)
    writer.writerow(default_values)

如果字典中没有键,则前两列将写入空值。

09-28 07:21