Pprint and Pformat of data formatting in Python

Source: Internet
Author: User
Tags pprint
Many times we need to format the data, do you have a headache with data formatting in Python? Pprint will do you a great favor.

The formatting used in the Pprint module can display data in a format that is parsed by the parser and is easy to read. The output is saved in a single line, but you can also use an indent representation when splitting multiple rows of data if necessary.

Import Sysimport Pprintpprint.pprint (Sys.path)

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


You can use Pformat if you just want to get the data, not the output data.

Import Sysimport pprintstr = Pprint.pformat (sys.path) Print str

The output is the same as above

  • 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.