Home > Blockchain >  replace dictionary single quotes to double
replace dictionary single quotes to double

Time:09-19

dic = [{'one':1, 'two':'t', 'three': {'three.1': 3.1, 'three.2': '3.2' }}]

Desire Output,

[{"one":1, "two":"t", "three": {"three.1": 3.1, "three.2": "3.2" }}]

Tried Code:

ast.literal_eval(json.dumps(dic[0]))

here, Json converting the quotes but also changing the type to str. So I applied ast to change type. at end getting the same output with single quotes.

Output of Json.dumps: json.dumps(dic[0])

'{"one": 1, "two": "t", "three": {"three.1": 3.1, "three.2": "3.2"}}'

Above pre and post single quotes are the issue.

CodePudding user response:

If you want to print the dictionary to terminal with double quotes, you can

>>> print(json.dumps(dic))
[{"one": 1, "two": "t", "three": {"three.1": 3.1, "three.2": "3.2"}}]

CodePudding user response:

Whats wrong with json.dumps(dic)?

  • Related