pandas尾部添加一条,如何停止在csv文件末尾写入空白行-Pandas

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!

解决方案

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="")

你可能感兴趣的:(pandas尾部添加一条)