pprint模块有一个名为pformat的命令,正是出于这个目的.
从文件中:
以字符串形式返回对象的格式化表示形式.缩进
示例:
>>> import pprint
>>> people = [
... {"first": "Brian", "last": "Kernighan"},
... {"first": "Dennis", "last": "Richie"},
... ]
>>> pprint.pformat(people, indent=4)
"[ { 'first': 'Brian', 'last': 'Kernighan'},\n { 'first': 'Dennis', 'last': 'Richie'}]"