[英]pandas dataframe to_csv writes blank line after each line
When I write a data frame to a csv if I use the "with open", I get a blank line after each line in the csv file: 如果我使用“ with open”将数据帧写入csv,则在csv文件中的每一行之后都会出现空白行:
df = pd.DataFrame(data=np.random.randint(0, 100, (4, 5)), columns=list('ABCDE'))
with open('test.csv', 'a') as f:
df.to_csv(f, header=False)
If I simply do: 如果我只是这样做:
df.to_csv('test.csv', header=False)
There is no blank line! 没有空行! What am I doing wrong?
我究竟做错了什么?
By default, the newline of : 默认情况下,以下行的换行符:
with open('test.csv', 'a') as f
is \\r\\n
是
\\r\\n
The newline in to_csv()
is \\n
. to_csv()
的换行符为\\n
。
You can try with : 您可以尝试:
with open('test.csv', 'a', newline = '\n') as f
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.