Question

Trying to convert given text file to .json file using a python script. I am trying to append double quotes as: output_str = '"'+input_str+'"' but it results in double quotes twice in the output.

i.e. if input_str = row
output_str = ""row"" instead of "row"
row.append(str)
writer = csv.writer(open("data.json",'wb'), delimiter=',')
writer.write(row)

Any suggestions?

No correct solution

OTHER TIPS

First of all your method seems fine. Second, I would use the json library to create json files:

import json
a = "this is a test"
json.dumps({"a": a})
>> '{"a": "this is a test"}'

Some code sample from the shell would be better, but you probably are seeing the shell quote around the string quote.

I recommend using the json library to automatically converting your dictionaries to a json file. It will take care of everything for you.

Your method is correct actually. Python may add quotes around the string itself, because it's the representation of the string object. But these quotes are just add when you display the string in the console, they don't exist otherwise!

>>> foo = "foobar"
>>> foo
'foobar'
^      ^
# quotes add via displayed by str() method of foo
>>> '"%s"' % foo
'"foobar"'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top