[英]replace dictionary single quotes to double
dic = [{'one':1, 'two':'t', 'three': {'three.1': 3.1, 'three.2': '3.2' }}]
Desire Output,欲望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.在这里,Json 转换引号但也将类型更改为 str。 So I applied ast to change type.所以我申请了 ast 来改变类型。 at end getting the same output with single quotes.最后得到带有单引号的相同 output 。
Output of Json.dumps: json.dumps(dic[0])
Output 的 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.上面的前后单引号是问题所在。
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"}}]
Whats wrong with json.dumps(dic)
? json.dumps(dic)
有什么问题?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.