python json有关问题


>>> mapping = {'a':1, 'b':34, 'c':789}
>>> import json
>>> json.dumps(mapping, indent=4, sort_keys=True)
'{\n    "a": 1,\n    "b": 34,\n    "c": 789\n}'

这里本想在输出字典时在每个元素前空出4格并提行,但为什么输出中的\n没有运行?

这是在交互模式下运行的显示,如果在cmd终端或者其他编辑器控制台中运行这个脚本文件,就可以看到显示效果了。

PS F:\2021\qa\ot1> ./t10
{
    "a": 1,
    "b": 34,
    "c": 789
}

如图 加个 print(json.dumps(mapping, indent=4, sort_keys=True))可实现

img