Home > Enterprise >  Using to_csv to generate a txt file without double quoting
Using to_csv to generate a txt file without double quoting

Time:08-11

I am trying to export pandas dataframe in txt. Some of data contains double quotes and I can't get it done correctly :

Desired output is a txt file of one column with this format

No Header
xxxxxxxx BG {"xxxxxx":"xxxx","xxxxx":"xxxxxx"}
yyyyyy BG {"yyyy":"yyyyy","yyyy":{"yyyy":""},"yyyy":{"yyyyy": {"yyyy":"yyyy","yyyyy":"yyyy"}},"yyy":"yyy","yyyy":"yyy","yyy":"yyyyyy","yyyy":"yyyyyy"}

The problem is that to_csv is doubling every "", I've already used

df.to_csv("df.txt",header=False,index=False,doublequote=False,escapechar=" ")

But by using it, the extra quote becomes a space, so I was wondering if there is a way to avoid that.

To put the quotes into the string I've used '"THIS FORMAT"'

CodePudding user response:

When you need complete control over the output, then you have to take control. You can't use the automated tools.

import pandas as pd
       
d = [
    ['xxxxxxxx BG {"xxxxxx":"xxxx","xxxxx":"xxxxxx"}' ],
    ['yyyyyy BG {"yyyy":"yyyyy","yyyy":{"yyyy":""},"yyyy":{"yyyyy": {"yyyy":"yyyy","yyyyy":"yyyy"}},"yyy":"yyy","yyyy":"yyy","yyy":"yyyyyy","yyyy":"yyyyyy"}']
]

df = pd.DataFrame(d,columns=['No Header'])
with open('xxx.txt','w') as out:
    for row in df.iterrows():
        print(row[1].iloc[0], file=out)
  • Related