Python - CSV模块,跳过行

时间:2017-05-31 13:05:26

标签: python csv

我尝试使用python创建.csv文件。 .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)

1 个答案:

答案 0 :(得分:0)

我错误地使用python 2语法而不是pythonn 3,我应该newline=''作为with open('persons.csv', 'w') as csvfile:中的参数来提供with open('persons.csv', 'w', newline='') as csvfile: