If I print a dictionary using pprint
, it always wraps strings around single quotes ('
):
>>> 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:
>>> from pprint import pprint
>>> pprint({'AAA': 1, 'BBB': 2, 'CCC': 3})
{"AAA": 1, "BBB": 2, "CCC": 3}
It looks like you are trying to produce JSON; if so, use the
json
module:The
pprint()
function produces Python representations, not JSON and quoting styles are not configurable. Don’t confuse the two syntaxes. JSON may at first glance look a lot like Python but there are more differences than just quoting styles:{...}
objects with key-value pairs,[...]
arrays,"..."
strings, numbers, booleans and nulls). Python data structures are far richer.true
andfalse
. Python uses title-case,True
andFalse
.null
to signal the absence of a value, Python usesNone
.\n
and\"
arbitrary codepoint escapes use\uXXXX
16-bit hexadecimal notation. Python 3 strings cover all of Unicode, and the syntax supports\xXX
,\uXXXX
, and\UXXXXXXXX
8, 16 and 32-bit escape sequences.If you want to produce indented JSON output (a bit like
pprint()
outputs indented Python syntax for lists and dictionaries), then addindent=4
andsort_keys=True
to thejson.dumps()
call:See http://stackoverflow.com/questions/12943819/how-to-python-prettyprint-a-json-file