点击数:7

写入csv文件发现每写一行都多出一行空白

# 原代码
import csv
    a1 = ['ID','姓名','年龄']
    a2 = ['001','小七',17]
    a3 = ['002','小八',18]
    with open('test_csv.csv','w') as f:
        writer = csv.writer(f)
        writer.writerows([a1,a2,a3])
原代码运行结果

修改成下面代码即可

import csv
    a1 = ['ID','姓名','年龄']
    a2 = ['001','小七',17]
    a3 = ['002','小八',18]
    with open('test_csv.csv','w',newline='') as f:  # 加了一个newline参数
        writer = csv.writer(f)
        writer.writerows([a1,a2,a3])
修改代码后运行结果

发表评论

电子邮件地址不会被公开。 必填项已用*标注