Home  >  Article  >  Backend Development  >  Parse formatted data using pprint module

Parse formatted data using pprint module

高洛峰
高洛峰Original
2017-03-16 17:33:372178browse

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)

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

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

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

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!

Statement:
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