python中的printio库。
printio的Python项目详细描述
printio是用python实现的mit许可的漂亮打印库。
使用python内置的字符串格式库格式化值以进行打印或显示。
- 格式规范迷你语言中的大多数字符串格式选项都可用。请参见此处的格式化选项:
- http://docs.python.org/release/3.1.2/library/string.html#format-specification-mini-language
功能
- Align values to the left, right, or center.
- Pad and fill values.
- Sign numerical values.
- Convert integers to float.
- Specify precision for floating values - values are rounded up.
- Convert numbers to percentage formats. Ex. 0.10 ~ 10.0000%
- Add column headers or utilize default headers for your values.
- Column widths are automatically sized based on maximum width of the values.
- Choose which columns to format in your values.
- Ability to print to ‘text’ similar to how MySQL displays output to the console.
概述
printio的主要功能:
- PrettyValue():
- Formats a single value to a string.
- PrettyValues():
- Formats a list of lists or dicts.
- format: will return a list of strings including the header.
- text: will return a string similar to MySQL’s console display format.
许可证
在麻省理工学院的许可下提供。
用法
首先,一些客房用品…
导入库
>>> from printio import PrettyValue >>> from printio import PrettyValues
创建要格式化的值的列表。
>>> lol = [] >>> lol.append([0, 'yhoo', 23.45]) >>> lol.append([1, 'goog', 200.4565]) >>> lol.append([2, 't', 1.00])
我们还要创建一个字典列表来格式化。
>>> keys = ['bar', 'symbol', 'close'] >>> lod = [dict(zip(keys, x)) for x in lol]
现在,我们开始谈正事吧…
设置宽度为10、居中对齐并填充“-”的字符串格式。
>>> value = 'yhoo' >>> pv = PrettyValue('^10', fill='-') >>> pv.format(value) ---yhoo---
以1的小数精度格式化浮点。
>>> value = 23.45599 >>> pv = PrettyValue('.1f') >>> pv.format(value) '23.5'
将浮点格式设置为百分比。
>>> value = 0.025 >>> pv = PrettyValue('.2%') >>> pv.format(value) '2.50%'
格式化列表。
>>> pv = PrettyValues() >>> for row in pv.format([lol[0]]): print row ['0', '1 ', '2 '] ['0', 'yhoo', '23.45']
设置听写格式。
>>> pv = PrettyValues() >>> pv.newcol('bar') >>> pv.newcol('symbol') >>> pv.newcol('close') >>> for row in pv.format([lod[0]]): print row ['bar', 'symbol', 'close'] ['0 ', 'yhoo ', '23.45']
设置列表的格式。
>>> pv = PrettyValues() >>> for row in pv.format(lol): print row ['0', '1 ', '2 '] ['0', 'yhoo', '23.45 '] ['1', 'goog', '200.4565'] ['2', 't ', '1.0 ']
同上,但这一次是在文本中。
>>> pv = PrettyValues() >>> results = pv.text(lol) >>> print results +---+------+----------+ | 0 | 1 | 2 | +---+------+----------+ | 0 | yhoo | 23.45 | | 1 | goog | 200.4565 | | 2 | t | 1.0 | +---+------+----------+
要添加更好的列名吗?
>>> pv.newcol(0, cname='Bar') >>> pv.newcol(1, cname='Symbol') >>> pv.newcol(2, cname='Close') >>> print pv.text(lol) +-----+--------+----------+ | Bar | Symbol | Close | +-----+--------+----------+ | 0 | yhoo | 23.45 | | 1 | goog | 200.4565 | | 2 | t | 1.0 | +-----+--------+----------+
只打印结束列,始终显示符号,格式精度为2。
>>> pv = PrettyValues() >>> pv.newcol(2, '+.2f', cname='Close') >>> print pv.text(lol) +---------+ | Close | +---------+ | + 23.45 | | +200.46 | | + 1.00 | +---------+
打印带有条形图和“关闭”数字设置的词典列表。
>>> pv = PrettyValues() >>> pv.newcol('bar', 'i') >>> pv.newcol('symbol') >>> pv.newcol('close', '.2f') >>> print pv.text(lod) +-----+--------+--------+ | bar | symbol | close | +-----+--------+--------+ | 0 | yhoo | 23.45 | | 1 | goog | 200.46 | | 2 | t | 1.00 | +-----+--------+--------+
路线图
- 除了列标题之外,还可以添加显示标题的选项。
- 添加autonum列功能。
- 将tb_html添加到html表的格式中。
- 将pre_html添加到格式<;pre>;html<;pre>;。
- 有关更多信息,请发送电子邮件:
- mike@taylortree.com