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

前端 未结 4 425
南旧
南旧 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:25

    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()
    

提交回复
热议问题