2017-05-31 174 views
0

我試圖用python創建一個.csv文件。 .csv文件已成功創建,但它在輸入以下結果的條目之間跳過一行。Python - CSV模塊,跳過行

['Name', 'Profession'] 
[] 
['Derek', 'Software Developer'] 
[] 
['Steve', 'Software Developer'] 
[] 
['Paul', 'Manager'] 
[] 

任何幫助解決這個問題將不勝感激。

import csv 

with open('persons.csv', 'w') as csvfile: 
    filewriter = csv.writer(csvfile, delimiter=',', 
          quotechar='|', quoting=csv.QUOTE_MINIMAL) 
    filewriter.writerow(['Name', 'Profession']) 
    filewriter.writerow(['Derek', 'Software Developer']) 
    filewriter.writerow(['Steve', 'Software Developer']) 
    filewriter.writerow(['Paul', 'Manager']) 


with open('persons.csv', 'rt') as f: 
    reader = csv.reader(f) 

    # read file row by row 
    for row in reader: 
     print(row) 
+2

檢查這個答案:https://stackoverflow.com/questions/3348460/CSV文件寫入與 - 蟒-具有空白線之間 - 的每行 –

回答

0

我被錯誤地使用Python 2語法,而不是pythonn 3,我應該有newline=''with open('persons.csv', 'w') as csvfile:參數給with open('persons.csv', 'w', newline='') as csvfile: