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

前端 未结 4 417
南旧
南旧 2021-01-02 03:44

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 f

4条回答
  •  庸人自扰
    2021-01-02 04:23

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

提交回复
热议问题