将字典中的单引号替换为双引号


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

期望输出,

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

尝试的代码:

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

在这里,Json转换引号,但也将类型更改为str。所以我应用ast来更改类型。最后用单引号得到相同的输出。

Json.dumps的输出:json.dumps(dic[0])

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

以上单引号是问题所在。

json.dumps(dic)出了什么问题?

如果你想用双引号将字典打印到终端,你可以

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

最新更新