【python】pprint格式化输出
【python】pprint格式化输出¶
起因¶
偶然看到这样一个 提问 。提问如下:
a={1: 2, 3: 4}
pprint.pprint(a)
{1: 2, 3: 4}
为什么不是格式化输出呢?
In [10]: pprint.pprint(sys.path)
['',
'/usr/bin',
'/usr/local/lib/python2.7/dist-packages/pysam-0.6-py2.7-linux-x86_64.egg',
'/usr/local/lib/python2.7/dist-packages/Cython-0.17-py2.7-linux-x86_64.egg',
'/usr/local/lib/python2.7/dist-packages/pip-1.2.1-py2.7.egg',
'/usr/lib/pymodules/python2.7',
'/usr/local/lib/python2.7/dist-packages/ruffus-2.2-py2.7
}
不是应该一行行输出字典吗?求解。
这个格式化输出我以前没用过,自己也就跟着去学习了一下。
分析¶
在本机上重新输入了问题的代码,果然如提问所叙。当我尝试把字典a改的较为复杂时,则可以实现分行了。 那么初步判断,应该是行宽的问题。查看pprint.pprint()的源码,
def pprint(object, stream=None, indent=1, width=80, depth=None):
"""Pretty-print a Python object to a stream [default is sys.stdout]."""
printer = PrettyPrinter(
stream=stream, indent=indent, width=width, depth=depth)
printer.pprint(object)
可以看到里面的参数width,这个从字面来看,应该是控制行宽的。 进一步来看一下源文件的注释。
"""Handle pretty printing operations onto a stream using a set of
configured parameters.
indent
Number of spaces to indent for each level of nesting.
width
Attempted maximum number of columns in the output.
depth
The maximum depth to print out nested structures.
stream
The desired output stream. If omitted (or false), the standard
output stream available at construction will be used.
"""
尝试把默认值80修改为1。发现就可以实现分行了。
a={1: 2, 3: 4}
pprint.pprint(a, width=1)
{1: 2,
3: 4}
至此,可以解答提问中的疑问了
应用¶
那么对于自己来说,这个pprint有什么用呢?
我想,这至少是一个很好的json解析工具。 每次面对相当繁杂的json串时,每每都要去上网使用 在线json格式化工具 ,相当之繁杂。 现在,可以尝试使用pprint来进行格式化。
def main():
import pprint
a = { "programmers": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },
{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
],
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
] }
pprint.pprint(a,width=1)
if __name__=="__main__":
main()
输出结果为:
{'authors': [{'firstName': 'Isaac',
'genre': 'science fiction',
'lastName': 'Asimov'},
{'firstName': 'Tad',
'genre': 'fantasy',
'lastName': 'Williams'},
{'firstName': 'Frank',
'genre': 'christian fiction',
'lastName': 'Peretti'}],
'musicians': [{'firstName': 'Eric',
'instrument': 'guitar',
'lastName': 'Clapton'},
{'firstName': 'Sergei',
'instrument': 'piano',
'lastName': 'Rachmaninoff'}],
'programmers': [{'email': 'aaaa',
'firstName': 'Brett',
'lastName': 'McLaughlin'},
{'email': 'bbbb',
'firstName': 'Jason',
'lastName': 'Hunter'},
{'email': 'cccc',
'firstName': 'Elliotte',
'lastName': 'Harold'}]}
perfect! 对于字符串可以做同样的处理。只是需要将字符串首先转换为Python对象。然后进行处理。
def main():
import pprint
a = "{'programmers': [{'lastName': 'McLaughlin', 'email': 'aaaa', 'firstName': 'Brett'}, {'lastName': 'Hunter', 'email': 'bbbb', 'firstName': 'Jason'}, {'lastName': 'Harold', 'email': 'cccc', 'firstName': 'Elliotte'}], 'musicians': [{'lastName': 'Clapton', 'firstName': 'Eric', 'instrument': 'guitar'}, {'lastName': 'Rachmaninoff', 'firstName': 'Sergei', 'instrument': 'piano'}], 'authors': [{'genre': 'science fiction', 'lastName': 'Asimov', 'firstName': 'Isaac'}, {'genre': 'fantasy', 'lastName': 'Williams', 'firstName': 'Tad'}, {'genre': 'christian fiction', 'lastName': 'Peretti', 'firstName': 'Frank'}]}"
b = eval(a)
pprint.pprint(b, width=1)
if __name__=="__main__":
main()
当然,你也可以修改输出流(stream参数),缩进(indent参数)等丰富你的功能。
尾声¶
再来看pprint的源码,突然看到开头有这样一段注释。
# Author: Fred L. Drake, Jr.
# fdrake@acm.org
#
# This is a simple little module I wrote to make life easier. I didn't
# see anything quite like it in the library, though I may have overlooked
# something. I wrote this when I was trying to read some heavily nested
# tuples with fairly non-descriptive content. This is modeled very much
# after Lisp/Scheme - style pretty-printing of lists. If you find it
# useful, thank small children who sleep at night.
感谢那些夜晚乖乖睡觉的孩子们。