在csv中写入数据时,我想写入特定的列,例如第7列或第12列。 我拥有的代码仅按顺序编写。
import csv
data = {}
data = ['sales','tax']
with open('test.csv','w',newline='') as fp:
a = csv.writer(fp,delimiter=',')
a.writerow(data)
实际结果:
coulmn 1 | coulmn 2 | coulmn 3 | coulmn 4 | coulmn 5
Sale Tax
预期结果:
coulmn 1 | coulmn 2 | coulmn 3 | coulmn 4 | coulmn 5
Sale Tax
答案 0 :(得分:0)
您可以创建一个空数组,仅填充所需的字段
data = [""] * 10
data[5] = "sales"
data[6] = "tax"
with open('test.csv','w',newline='') as fp:
a = csv.writer(fp,delimiter=',')
a.writerow(data)