Code Sample, a copy-pastable example if possible
import numpy as np
import pandas as pd
s = pd.Series(np.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])
s.to_csv('test.txt',sep=' ',float_format='%12.5f')
Problem description
The contents of test.txt is
a " 0.09048"
b " 0.13346"
c " 0.13251"
d " 0.00243"
e " 0.03104"
not sure where is the " from
Expected Output
same result without "
Output of pd.show_versions()
python: 3.5.0.final.0
OS:Linux
pandas: 0.20.3
Comment From: TomAugspurger
Hmm I think everything is working properly here. To make things easier to explain, we'll do print(s.to_frame().to_csv(...)
, since Series.to_csv
doesn't have all the options DataFrame.to_csv
does yet (we have another issue for that).
With the options you gave, we need the quotes, else we generate an invalid CSV, since the header row would have just a single field and the rest of the rows would have many (the number of spaces).
In [24]: print(s.to_frame().to_csv(sep=" ", float_format='%12.5f'))
0
a " 0.37333"
b " 1.72104"
c " 0.01158"
d " -0.38504"
e " 0.81918"
To say you want no quoting no matter what, use csv.QUOTE_NONE
In [32]: print(s.to_frame().to_csv(sep=' ',float_format='%12.5f', quoting=csv.QUOTE_NONE))
...
Error: need to escape, but no escapechar set
And it's complaining since you'd generate an invalid CSV that way, so you need an escapechar:
In [33]: print(s.to_frame().to_csv(sep=' ',float_format='%12.5f', quoting=csv.QUOTE_NONE, escapechar='\\'))
0
a \ \ \ \ \ 0.37333
b \ \ \ \ \ 1.72104
c \ \ \ \ \ 0.01158
d \ \ \ \ -0.38504
e \ \ \ \ \ 0.81918
Comment From: tonymonk007
Now I am clear. Thanks a lot.