Parse formatted data using pprint module

高洛峰
Release: 2017-03-16 17:33:37
Original
2183 people have browsed it

The formatting used in Python's pprint module correctly displays 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, in Indentation can also be used when splitting multi-line data.

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

Running results:

['',
 '/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']
Copy after login
Copy after login

If you just want to obtain data rather than output data, you can also use pformat

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

to run Result:

['',
 '/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']
Copy after login
Copy after login

The running result is the same~~

The above is the detailed content of Parse formatted data using pprint module. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!