列表到CSV编写器Python
我正在尝试写入CSV(我已经阅读了文档/大多数其他线程)。但对我来说,只有最后一行被复制到CSV。这就是我所拥有的列表到CSV编写器Python,python,csv,Python,Csv,我正在尝试写入CSV(我已经阅读了文档/大多数其他线程)。但对我来说,只有最后一行被复制到CSV。这就是我所拥有的 def writetocsv(l): #convert the set to the list b = list(l) #print b #checking print b, it prints all values of b with open("eggs.csv",'wb') as f: w = csv.writer(f)
def writetocsv(l):
#convert the set to the list
b = list(l)
#print b #checking print b, it prints all values of b
with open("eggs.csv",'wb') as f:
w = csv.writer(f)
w.writerows(b)
我的输入(b)是
我对CSV的期望是
a.
b,
c
我得到的是
c
使用for循环迭代列表
b
,使用writerow
写入单个值。获取此w.writerows(值)\u csv.Error:预期序列已尝试此早期使用writerow
而不是writerows
。编辑回复不走运,它只写最后一行,其他的writerow
都不能用于列表。您不需要循环。
尝试不使用循环无效。我认为循环每次都会覆盖第一行。因此,这里只有最后一行。您是如何说的,您正在将值传递给函数的?是:writetocsv([a,b,c])
?请显示具有函数的调用
的代码。还有a、b和c的值。
c
def writetocsv(l):
#convert the set to the list
b = list(l)
#print b #checking print b, it prints all values of b
with open("eggs.csv",'wb') as f:
w = csv.writer(f)
for value in b:
w.writerow(value)