如何停止在 csv 文件末尾写一个空行 - pandas
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39237755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to stop writing a blank line at the end of csv file - pandas
提问by medev21
When saving the data to csv, data.to_csv('csv_data', sep=',', encoding='utf-8', header= False, index = False)
, it creates a blank line at the end of csv file.
将数据保存到 csv, 时data.to_csv('csv_data', sep=',', encoding='utf-8', header= False, index = False)
,它会在 csv 文件的末尾创建一个空行。
How do you avoid that?
你如何避免这种情况?
It's got to do with the line_terminator
and it's default value is n
, for new line.
它与line_terminator
和它的默认值有关n
,对于新行。
Is there a way to specify the line_terminator
to avoid creating a blank line at the end, or do i need to read the csv file, remove the blank line and save it?
有没有办法指定line_terminator
避免在最后创建空行,或者我是否需要读取 csv 文件,删除空行并保存?
Not familiar with pandas. Your help will be appreciated, thanks in advance!
对Pandas不熟悉。您的帮助将不胜感激,提前致谢!
回答by BKS
One way would be to save data except the last entry,with default line_terminator
(\n
) and append the last line with line_terminator=""
.
一种方法是使用默认值line_terminator
( \n
)保存除最后一个条目之外的数据,并在最后一行附加line_terminator=""
.
data1 = data.iloc[0:len(data)-1]
data2 = data.iloc[[len(data)-1]]
data1.to_csv('csv_data', sep=',', encoding='utf-8', header= False, index = False)
data2.to_csv('csv_data', sep=',', encoding='utf-8', header= False, index = False,mode='a',line_terminator="")
回答by Jared
For some reason, the line terminator did not work when I tried it. (It gave an error, saying line_terminator is an unrecognized keyword argument.)
出于某种原因,当我尝试时行终止符不起作用。(它给出了一个错误,说 line_terminator 是一个无法识别的关键字参数。)
However, this will do the trick:
但是,这可以解决问题:
df.to_csv(path)
with open(path) as f:
lines = f.readlines()
last = len(lines) - 1
lines[last] = lines[last].replace('\r','').replace('\n','')
with open(path, 'w') as wr:
wr.writelines(lines)
回答by DonkeyKong
file_out = r'c:\your_output_file_path\file_name.csv'
df.to_csv(file_out)
file_data = open(file_out, 'rb').read()
open(file_out, 'wb').write(file_data[:-2])
df.to_csv() function has a parameter called line_terminator with a default value of '\n'. This new line character is the issue at hand.
df.to_csv() 函数有一个名为 line_terminator 的参数,默认值为 '\n'。这个新行字符是手头的问题。
The code above:
1) writes the dataframe to file as normal
2) opens the file and reads in the bytes data to the file_data variable
3) writes the file_data variable back out to the same file but trims off the '\n' with the splice: file_data[:-2]
上面的代码:
1) 像往常一样将数据帧写入文件
2) 打开文件并将字节数据读入 file_data 变量
3) 将 file_data 变量写回同一个文件,但用拼接:file_data[:-2]
回答by Luke Corrigall
A more efficient way is to open the file first, write to that stream, then remove the last newline:
更有效的方法是先打开文件,写入该流,然后删除最后一个换行符:
import os
with open('csv_data', 'wb') as dst:
data.to_csv(wb, sep=',', encoding='utf-8', header= False, index = False)
dst.seek(-1, os.SEEK_END) # <---- 1 : len('\n')
dst.truncate()