如何解决在写入csv格式文件时出现空行的问题?
问题陈述
在往csv文件中写东西时,出现下面的情况:
可以看到,每写入一行数据,就出现了空行。
我的代码:
问题解决
import csv
path = "C:/Users/ASUS/Desktop/aa/xigua.txt"
data = []
with open(path,'r') as f:
data = f.read().split('\n')
# print(data)
path2 = "C:/Users/ASUS/Desktop/aa/xigua.csv"
with open(path2,'w', newline='') as fp:
fp_write = csv.writer(fp)
for d in data:
fp_write.writerow(d.split(','))