2014-09-30 103 views
0

我想写入CSV(我已阅读文档/大多数其他线程)。但对我而言,只有最后一行被复制到CSV。这是我列表CSV作家Python

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)的

[a,b,c] 

我期望在CSV是

a. 
b, 
c 

我得到的是

c 
+3

编写个人value你是怎么说的,你将值传递给函数?是:'writetocsv([a,b,c])'?请显示具有调用功能的代码。而且值a,b和c也具有。 – 2014-09-30 06:37:50

回答

2
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) 

使用for循环遍历列表b,使用writerow

+0

没有工作。得到这个 w.writerows(值) _csv.Error:期望的序列 早些时候试过 – smushi 2014-09-30 06:15:41

+0

使用'writerow'而不是'writerows'。编辑回复 – nu11p01n73R 2014-09-30 06:18:10

+0

仍然没有运气,它只是写最后一行,没有别的 – smushi 2014-09-30 06:20:19