'Additional columns added to saved CSV”翻译为中文意思为'保存的CSV文件中添加了额外的列”,解决方法示例代码如下:
import csv
data = [['id', 'name', 'age', 'gender'], [1, '张三', 22, '男'], [2, '李四', 23, '女']]
with open('test.csv', 'w', newline='') as f: writer = csv.writer(f) # 写入数据 writer.writerows(data)
with open('test.csv', 'r', newline='') as f: reader = csv.reader(f) rows = [row for row in reader]
rows[0].append('score')
for row in rows[1:]: row.append('90')
with open('test.csv', 'w', newline='') as f: writer = csv.writer(f) writer.writerows(rows)