[python2] python 打印表格 prettytable
rpm包:
[root@D129 cli]# yum info python-prettytable Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * epel: mirrors.huaweicloud.com * extras: mirrors.cn99.com * updates: mirrors.cn99.com Available Packages Name : python-prettytable Arch : noarch Version : 0.7.2 Release : 3.el7 Size : 37 k Repo : base/7/x86_64 Summary : Python library to display tabular data in tables URL : http://pypi.python.org/pypi/PrettyTable License : BSD Description : PrettyTable is a simple Python library designed to make it quick and easy to : represent tabular data in visually appealing ASCII tables. It was inspired by : the ASCII tables used in the PostgreSQL shell psql. PrettyTable allows for : selection of which columns are to be printed, independent alignment of columns : (left or right justified or centred) and printing of "sub-tables" by specifying : a row range. [root@D129 cli]#
官网:
https://code.google.com/archive/p/prettytable/
文档:
https://code.google.com/archive/p/prettytable/wikis/Tutorial.wiki
小例子:
from prettytable import PrettyTable ... ... x = PrettyTable(["port", "rx_pps", "rx_bps", "rx_drops", "rx_errors", "tx_pps", "tx_bps", "tx_drops", "tx_errors"]) x.align["port"] = "l"
... ... for i in range(length): x.add_row([new_item.port, rpps, rbps, rx_drop, rx_errs, tpps, tbps, tx_drop, tx_errs]) sys.stderr.write(str(x) + "\n")
... ...
输出:
[root@A04-R08-I244-12-9283C72 tong]# ./ovs-stat.py 1>/dev/null +-----------------+--------+--------+----------+-----------+--------+--------+----------+-----------+ | port | rx_pps | rx_bps | rx_drops | rx_errors | tx_pps | tx_bps | tx_drops | tx_errors | +-----------------+--------+--------+----------+-----------+--------+--------+----------+-----------+ | dpdk0 | 0.0 | 0.0 | 0 | 0 | 0.0 | 0.0 | 0 | 0 | | port-uzwlar0rxo | 0.0 | 0.0 | 0 | 0 | 0.0 | 0.0 | 0 | 0 | | vx10.0.57.13 | 0.0 | 0.0 | 0 | 0 | 0.0 | 0.0 | 0 | 0 | | vx10.0.54.235 | 0.0 | 0.0 | 0 | 0 | 0.0 | 0.0 | 0 | 0 | +-----------------+--------+--------+----------+-----------+--------+--------+----------+-----------+ +-----------------+---------------+--------------+----------+-----------+---------------+--------------+----------+-----------+ | port | rx_pps | rx_bps | rx_drops | rx_errors | tx_pps | tx_bps | tx_drops | tx_errors | +-----------------+---------------+--------------+----------+-----------+---------------+--------------+----------+-----------+ | dpdk0 | 1.54353919126 | 180.20820058 | 0 | 0 | 1.54353919126 | 183.68116376 | 0 | 0 | | port-uzwlar0rxo | 0.0 | 0.0 | 0 | 0 | 0.0 | 0.0 | 0 | 0 | | vx10.0.57.13 | 0.0 | 0.0 | 0 | 0 | 0.0 | 0.0 | 0 | 0 | | vx10.0.54.235 | 0.0 | 0.0 | 0 | 0 | 0.0 | 0.0 | 0 | 0 | +-----------------+---------------+--------------+----------+-----------+---------------+--------------+----------+-----------+