문제

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?

올바른 솔루션이 없습니다

다른 팁

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"'
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top