0

I try the following code to write csv.

df = pd.DataFrame({'A': [1, 2, 3, 4, 5]})

with open('D:/Test/1.csv', 'w') as f:
    df.to_csv(f, index=False)

df.to_csv('D:/Test/2.csv', index=False)

The 1.csv would have empty row between each row, and 2.csv looks fine. Anyone knows why? I am using pandas '0.24.2'

JOHN
  • 871
  • 1
  • 12
  • 24
  • 1
    `with open('D:/Test/1.csv', 'w', newline='') as f:` – roganjosh Apr 11 '19 at 21:49
  • 1
    There is, to my knowledge, absolutely no point in passing the file object here. Pandas is dealing with the carriage return issue on Windows for you. Just use `df.to_csv('D:/Test/2.csv', index=False)` – roganjosh Apr 11 '19 at 21:51
  • 3
    @Greenstick it's not a case of duplication of data. The regular `csv` module will do the same in Windows if you don't specify `newline=''` – roganjosh Apr 11 '19 at 21:54
  • @roganjosh thanks, that's good to know – JOHN Apr 11 '19 at 22:03

0 Answers0