pprint and pformat for data formatting in Python

高洛峰
Release: 2016-10-19 14:54:51
Original
1917 people have browsed it

Many times we need to format data. Have you ever had a headache with data formatting in python? pprint will help you a lot

The formatting used in the pprint module can correctly display the data in a format that can be parsed by the parser and is easy to read. The output is saved in a single line, but If necessary, indentation can also be used when splitting multiple lines of data.

import sys
import pprint
pprint.pprint(sys.path)
Copy after login

Running results:


['',

'/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']


If you just want to get data instead of outputting data, you can also use pformat

import sys
import pprint
str = pprint.pformat(sys.path)
print str
Copy after login

The output result is the same as above


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template