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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java引用MediaPlayer中断脚本?NullPointerException(JavaFX)   用于查找字符串的java正则表达式模式包含一些单词,而没有其他单词   使用Jfreechart和Runnable接口在java上显示实时数据的多线程处理   java如何在获取imei信息时获取dialogFragment中的上下文   安卓 Java在一个循环中打印每个i   apache kafka主题上的java流媒体没有输出   java是在Akka中模拟大量HTTP连接的最佳方法   java如何使用ibatis注释进行批插入   硬件如何在Linux、Windows和Mac上使用Java+JNI检索硬盘的唯一ID   java声明实例变量并实现getter方法?   java正则表达式和新行   java有没有办法使用安卓的Play Core安装本地apk?   java将Graphics2D绘制到另一个Graphics2D   使用Spring和Glassfish的JtaTransactionManager的java JNDI初始上下文   java是一个JPanel问题   网络Java UDP数据包丢失。50%   java如何从字符串正则表达式中提取数据   java 安卓不录制或编码声音