Pprint and pformat for data formatting in Python

Source: Internet
Author: User
Tags pprint
In many cases, we need to format the data. do you have a headache in formatting python data? Pprint will help you a lot with the formatting that is used in the pprint module. we often need to format the data. do you have a headache for formatting the data in python? Pprint will help you a lot

The format used in the pprint module can display data in a correct format, which can be parsed by the parser and easy to read. the output is stored in a single row. if necessary, you can use indentation to separate multiple rows of data.

import sysimport pprintpprint.pprint(sys.path)

Running result:


['',

'/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 only want to obtain data rather than output data, you can also use pformat

import sysimport pprintstr = pprint.pformat(sys.path)print str

The output result is the same as the preceding one.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.