How to force a new line when appending to a csv using python pandas .to_csv

前端 未结 3 972
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-12 17:38

When appending to csv, my first line is starting on the existing last line rather than a new line.

I keep searching SO, but I am just finding the basic use of openi

3条回答
  •  清歌不尽
    2021-01-12 18:13

    I had a similar problem and after a god bit of searching, I didn't find any simple/elegant solution. The minimal fix that worked for me is:

    import pandas as pd
    
    with open('foo.csv') as f:
        f.write('\n')    
    mydf.to_csv('foo.csv', index = False, header = False, mode='a')
    

提交回复
热议问题