2014-09-30 86 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