to_csv() inserts extra newlines

12 views
Skip to first unread message

Mahmood Naderan

unread,
Jan 3, 2022, 12:44:29 PMJan 3
to spyder
Hi,

I am using Spyder IDE 5.2.0 with the default settings and python version, 3.7.9. However, I have noticed that the csv writer inserts extra new lines in the output file. Example is shown below:

    import pandas as pd
    import csv df = pd.read_csv('test.csv', usecols=['ID','Name','Metric','Unit','Value'])
    f1 = open('test2.csv', 'w')
    df.to_csv(f1)
    f1.close()

The input is

    ID,Name,Metric,Unit,Value
   0,K1,M1,usecond,200
   0,K1,M2,,5
   1,K2,M1,msecond,1
   1,K2,M2,,10
   2,K2,M1,msecond,500
   2,K2,M2,,8

But the final output looks like

,ID,Name,Metric,Unit,Value

0,0,K1,M1,usecond,200

1,0,K1,M2,,5

2,1,K2,M1,msecond,1

3,1,K2,M2,,10

4,2,K2,M1,msecond,500

5,2,K2,M2,,8




Any idea to fix that? The system information is shown below

Spyder IDE 5.2.0 | Python 3.7.9 64-bit | Qt 5.12.10 | PyQt5 5.12.3 | Windows 10



Regards,
Mahmood
Reply all
Reply to author
Forward
0 new messages