pprint():如何使用双引号来显示字符串?

时间:2021-07-19 18:06:38

If I print a dictionary using pprint, it always wraps strings around single quotes ('):

如果我用pprint打印一个字典,它总是用单引号括起来('):

>>> from pprint import pprint
>>> pprint({'AAA': 1, 'BBB': 2, 'CCC': 3})
{'AAA': 1, 'BBB': 2, 'CCC': 3}

Is there any way to tell pprint to use double quotes (") instead? I would like to have the following behaviour:

有没有办法让pprint用双引号代替双引号?我希望有以下行为:

>>> from pprint import pprint
>>> pprint({'AAA': 1, 'BBB': 2, 'CCC': 3})
{"AAA": 1, "BBB": 2, "CCC": 3}

1 个解决方案

#1


14  

It looks like you are trying to produce JSON; if so, use the json module:

看起来您正在尝试生成JSON;如果是,使用json模块:

>>> import json
>>> print json.dumps({'AAA': 1, 'BBB': 2, 'CCC': 3})
{"AAA": 1, "BBB": 2, "CCC": 3}

#1


14  

It looks like you are trying to produce JSON; if so, use the json module:

看起来您正在尝试生成JSON;如果是,使用json模块:

>>> import json
>>> print json.dumps({'AAA': 1, 'BBB': 2, 'CCC': 3})
{"AAA": 1, "BBB": 2, "CCC": 3}