Python中数据名目化的pprint和pformat
许多时候我们需要对数据举办名目化,你有没有对python中的数据名目化感想头疼呢?pprint会帮你很大的忙
pprint模块中利用的名目化可以凭据一种名目正确的显示数据, 这种名目即可被理会器理会, 又很易读. 输出生存在一个单行内, 但假如有须要, 在支解多行数据时也可利用缩进暗示.
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
import sys import pprint str = pprint.pformat(sys.path) print str
输出功效和上面是一样的