Python3.2官方文档翻译--输出格式化
2017-04-17 10:59
871 查看
第八章 标准库二
第二部分涵盖了很多更能满足专业开发者需求的高级模块。这些模块在小脚本中非常少出现。8.1 输出格式化
Reprlib模块为大型的或深度嵌套的容器缩写显示提供了repr()函数的一个定制版本号。>>> import reprlib
>>> reprlib.repr(set(’supercalifragilisticexpialidocious’))
"set([’a’, ’c’, ’d’, ’e’, ’f’, ’g’, ...])"
Pprint模块提供了对输出内置函数和用户定义对象更加复杂的控制。这样的方式是解释器可以读懂的。
当结果多于一行时,“完美打印机”就会添加行中断和随进,一边更清晰的显示数据结构。
>>> 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.
Local模块用来訪问特殊数据格式的文化数据库。Local的分组格式化函数属性为数字的分组分隔格式化提供了直接的方法。
>>> 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’
相关文章推荐
- Python3.2官方文档翻译--输出格式化
- Python3.2官方文档教程---格式化输出
- 格式化输出Python3.2官方文档教程
- Python3.2官方文档翻译--生成器以及生成器表达式
- Python3.2官方文档翻译--使用二进制数据记录布局和多线程
- Python3.2 官方文档翻译--错误与异常
- Python3.2官方文档翻译--作用域和命名空间实例
- Python3.2官方文档翻译--异常抛出和自定义异常
- Python3.2官方文档翻译--标准库概览(一)
- Python3.2官方文档翻译--异常处理
- Python3.2官方文档翻译--作用域和命名空间
- Python3.2官方文档翻译---类一些说明
- Python3.2官方文档翻译--类备注和异常也是类
- Python3.2官方文档翻译--继承
- Python3.2官方文档翻译--异常抛出和自定义异常
- Python3.2官方文档翻译--模板
- Python3.2官方文档翻译-列表工具和十进制浮点数计算
- Python3.2官方文档翻译--实例对象和方法对象
- Python3.2官方文档翻译--类总介绍
- Python3.2官方文档翻译--清理动作