换句话说,什么是 sprintf 等价于 pprint?
最佳答案
pprint模块有一个名为 pformat 的函数,就是为了这个目的。
来自文档:
Return the formatted representation of object as a string. indent, width and depth will be passed to the PrettyPrinter constructor as formatting parameters.
例子:
>>> 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'}]"
关于python - 如何让 python 的 pprint 返回一个字符串而不是打印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/521532/