11.1. 输出格式

reprlib 模块为大型的或深度嵌套的容器缩写显示提供了 repr() 函数的一个定制版本:

>>> import reprlib
>>> reprlib.repr(set("supercalifragilisticexpialidocious"))
"set(["a", "c", "d", "e", "f", "g", ...])"

pprint 模块给老手提供了一种解释器可读的方式深入控制内置和用户自定义对象的打印。当输出超过一行的时候,“美化打印(pretty printer)”添加断行和标识符,使得数据结构显示的更清晰:

>>> import pprint
>>> t = [[[["black", "cyan"], "white", ["green", "red"]], [["magenta",
...     "yellow"], "blue"]]]
...
>>> pprint.pprint(t, width=30)
[[[["black", "cyan"],
 "white",
 ["green", "red"]],
 [["magenta", "yellow"],
 "blue"]]]

textwrap 模块格式化文本段落以适应设定的屏宽:

>>> import textwrap
>>> doc = """The wrap() method is just like fill() except 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()
except that it returns a list of strings
instead of one big string with newlines
to separate the wrapped lines.

locale 模块按访问预定好的国家信息数据库。locale 的格式化函数属性集提供了一个直接方式以分组标示格式化数字:

>>> 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)
"1,234,567"
>>> locale.format_string("%s%.*f", (conv["currency_symbol"],
...                      conv["frac_digits"], x), grouping=True)
"$1,234,567.80"
文章导航