Python3.2 official documentation translation-output formatting

Source: Internet
Author: User

Chapter 8 standard library II

The second part covers many advanced modules that can better meet the needs of professional developers. These modules rarely appear in small scripts.

8.1 output formatting

The Reprlib module provides a customized version of the repr () function for large-scale or deeply nested container abbreviation display.

>>> Import reprlib

>>> Reprlib. repr (set ('supercalifragilisticexpialidocious '))

"Set (['A', 'C', 'D', 'E', 'F', 'G',...])"

The Pprint module provides more complex control over the output built-in functions and user-defined objects. This method can be understood by the interpreter. When there are more than one row of results, the "perfect printer" will increase the line interruption and forward, while the data structure is clearer.

>>> Import pprint

>>> T = [[['black', 'cyany'], 'white', ['green', 'red'], [['magenta ',

... 'Yellow'], 'blue']

...

>>> Pprint. pprint (t, width = 30)

[[['Black', 'cyany'],

'White ',

['Green', 'red'],

[['Magenta', 'yellow'],

'Blue']

The Textwrap module formats text paragraphs to adapt to the width of the screen.

>>> Import textwrap

>>> Doc = "The wrap () method is just like fill () handle t that it returns

... A list of strings instead of one big string with newlines to separate

... The wrapped lines ."""

...

>>> Print (textwrap. fill (doc, width = 40 ))

The wrap () method is just like fill ()

Statement t that it returns a list of strings

Instead of one big string with newlines

To separate the wrapped lines.

The Local module is used to access cultural databases in special data formats. The Group Formatting Function of Local provides a direct method for grouping and formatting numbers.

>>> Import locale

>>> Locale. setlocale (locale. LC_ALL, 'English _ United States.1252 ')

'English _ United States.1252'

>>> Conv = locale. localeconv () # get a mapping of conventions

> X = 1234567.8

>>> Locale. format ("% d", x, grouping = True)

'123'

>>> Locale. format_string ("% s %. * f", (conv ['currency _ symbol'],

... Conv ['frac _ digits '], x), grouping = True)

'$1,234,567.80'

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.