Reputation: 1061
I have a sample csv as following:
x1, x2, x3
01, 02, 03
11, 22, 33
Reading it into R and write.csv
generates:
"x1","x2","x3"
1,2,3
11,22,33
However, when I read it in python
and try to write via to_csv
here is what I get:
x1, x2, x3
1,2,3
11,22,33
I don't get the quotation marks in the header line of the output in python
's to_csv
. In other words, what I want is the csv
in earlier format but generated with python
.
Any suggestion?
Upvotes: 0
Views: 53
Reputation: 1868
First, let's read your csv file correctly saying that data or headers can be separated by a ,
and possibly one or more spaces \s+
import csv
import pandas as pd
df = pd.read_csv("data.csv", sep=r',\s+', engine='python')
This gives us
x1 x2 x3
0 1 2 3
1 11 22 33
Then we save to csv enforcing quoting non-numeric fields and drop the indexes (see doc)
df.to_csv("data_bis.csv", index=False, quoting=csv.QUOTE_NONNUMERIC)
This gives
"x1","x2","x3"
1,2,3
11,22,33
Upvotes: 1