Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop writing a blank line at the end of csv file - pandas

Tags:

python

pandas

csv

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!

like image 294
medev21 Avatar asked Aug 30 '16 22:08

medev21


3 Answers

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]

like image 131
DonkeyKong Avatar answered Oct 19 '22 18:10

DonkeyKong


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="")
like image 8
kanatti Avatar answered Oct 19 '22 19:10

kanatti


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)
like image 3
Jared Avatar answered Oct 19 '22 19:10

Jared