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.
How do you avoid that?
It's got to do with the line_terminator
and it's default value is n
, for new line.
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?
Not familiar with pandas. Your help will be appreciated, thanks in advance!
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.
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]
One way would be to save data except the last entry,with default line_terminator
(\n
) and append the last line with 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="")
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.)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With