如何使用python将多行附加到csv文件

时间:2017-12-01 21:05:50

标签: python csv append row

我试图将用户输入的数据附加到行,但是我没有运气。

Counter是用户定义的全局变量,我在代码的其他地方创建了它。

当附加数据时,而不是移动到另一行,它只是一遍又一遍地重写相同的行,作为python的新用户,我不知道我做错了什么。

def AppendCsv(): 
    NumberOfNamesToBeAdded()
    with open ('Graph.csv', 'a') as graphFile:
        graphFileWriter=csv.writer(graphFile)
        for x in counter:
             graphFileWriter.writerow([ID, Name,Cost,Date1]) 

1 个答案:

答案 0 :(得分:0)

这取决于Cannot redeclare block-scoped variable 'customerElements'中的{1}}。在counter中使用range,就像这样:

counter

结果如下:

import csv

counter = range(3)

with open('Graph.csv', 'a', newline='') as graphFile:
    graphFileWriter = csv.writer(graphFile)
    for x in counter:
        graphFileWriter.writerow([1,2,3])

您可能还想在1,2,3 1,2,3 1,2,3 声明中插入, newline=''