很多时候我们需要对数据进行格式化,你有没有对python中的数据格式化感到头疼呢?pprint会帮你很大的忙
pprint模块中使用的格式化可以按照一种格式正确的显示数据, 这种格式即可被解析器解析, 又很易读. 输出保存在一个单行内, 但如果有必要, 在分割多行数据时也可使用缩进表示.
1
2
3
|
import sys import pprint pprint.pprint(sys.path) |
运行结果:
[”,
‘/usr/local/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-x86_64.egg’,
‘/usr/local/lib/python2.7/site-packages/web.py-0.36-py2.7.egg’,
‘/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.7.5-py2.7-linux-x86_64.egg’,
‘/usr/local/lib/python2.7/site-packages/thrift-0.8.0-py2.7-linux-x86_64.egg’,
‘/usr/local/lib/python2.7/site-packages/setuptools-0.9.8-py2.7.egg’,
‘/usr/local/lib/python27.zip’,
‘/usr/local/lib/python2.7’,
‘/usr/local/lib/python2.7/plat-linux2’,
‘/usr/local/lib/python2.7/lib-tk’,
‘/usr/local/lib/python2.7/lib-old’,
‘/usr/local/lib/python2.7/lib-dynload’,
‘/usr/local/lib/python2.7/site-packages’,
‘/usr/local/lib/python2.7/site-packages/PIL’]
如果仅仅想获得数据而不是输出数据也可以用pformat
1
2
3
4
|
import sys import pprint str = pprint.pformat(sys.path) print str |
输出结果和上面是一样的
暂无评论内容