我的 CSV 看起来像
John,Bomb,Dawn
3,4,5
3,4,5
3,4,5
我想在前面添加 ID 列,如下所示:
ID,John,Bomb,Dawn
1,3,4,5
2,3,4,5
3,3,4,5
使用枚举函数,但我不知道如何。到目前为止,这是我的代码:
import csv
with open("testi.csv", 'rb') as input, open('temp.csv', 'wb') as output:
reader = csv.reader(input, delimiter = ',')
writer = csv.writer(output, delimiter = ',')
all = []
row = next(reader)
row.append('ID')
all.append(row)
count = 0
for row in reader:
count += 1
while count:
all.append(row)
row.append(enumerate(reader, 1))
break
writer.writerows(all)
输出全部错误:
John,Bomb,Dawn,ID
3,4,5,<enumerate object at 0x7fb2a5728d70>
3,4,5,<enumerate object at 0x1764370>
3,4,5,<enumerate object at 0x17643c0>
所以 ID 出现在最后,而它应该出现在开头,而且它甚至没有执行 1、2、3。出现一些奇怪的错误。