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'