Using to_csv to generate a txt file without double quoting

Question:

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"'

Asked By: manueldiaz97

||

Answers:

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)
Answered By: Tim Roberts
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.