Python通过prettytable模块可以将输出内容如表格方式整齐的输出。
使用该模块,用户可以在日志中输出清晰的日志文档。
Python通过prettytable模块可以将输出内容如表格方式整齐的输出。
使用该模块,用户可以在日志中输出清晰的日志文档。
sortby - name of field to sort rows by
reversesort - True or False to sort in descending or ascending order
int_format - controls formatting of integer data
float_format - controls formatting of floating point data
add_row(row)
"""Add a row to the table
Arguments:
row - row of data, should be a list with as many elements as the table
has fields"""
del_row(row_index)
"""Delete a row to the table
Arguments:
row_index - The index of the row you want to delete. Indexing starts at 0."""
add_column(fieldname, column, align="c", valign="t")
"""Add a column to the table.
Arguments:
fieldname - name of the field to contain the new column of data
column - column of data, should be a list with as many elements as the
table has rows
align - desired alignment for this column - "l" for left, "c" for centre and "r" for right
valign - desired vertical alignment for new columns - "t" for top, "m" for middle and "b" for bottom"""
clear_rows()
"""Delete all rows from the table but keep the current field names"""
clear()
"""Delete all rows and field names from the table, maintaining nothing but styling options"""
示例如下:
from prettytable import *
x = PrettyTable(["City name", "Area", "Population", "Annual Rainfall"])
x.sortby = "Population"
x.reversesort = True
x.int_format["Area"] = "04d"
x.float_format = "6.1f"
x.align["City name"] = "l" # Left align city names
x.add_row(["Adelaide", 1295, 1158259, 600.5])
x.add_row(["Brisbane", 5905, 1857594, 1146.4])
x.add_row(["Darwin", 112, 120900, 1714.7])
x.add_row(["Hobart", 1357, 205556, 619.5])
x.add_row(["Sydney", 2058, 4336374, 1214.8])
x.add_row(["Melbourne", 1566, 3806092, 646.9])
x.add_row(["Perth", 5386, 1554769, 869.4])
print x
+-----------+-------+------------+-----------------+ | City name | Area | Population | Annual Rainfall | +-----------+-------+------------+-----------------+ | Sydney | 2058d | 4336374 | 1214.8f | | Melbourne | 1566d | 3806092 | 646.9f | | Brisbane | 5905d | 1857594 | 1146.4f | | Perth | 5386d | 1554769 | 869.4f | | Adelaide | 1295d | 1158259 | 600.5f | | Hobart | 1357d | 205556 | 619.5f | | Darwin | 0112d | 120900 | 1714.7f | +-----------+-------+------------+-----------------+
# 通过行添加
row = PrettyTable()
row.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
row.add_row(["Adelaide",1295, 1158259, 600.5])
row.add_row(["Brisbane",5905, 1857594, 1146.4])
row.add_row(["Darwin", 112, 120900, 1714.7])
row.add_row(["Hobart", 1357, 205556, 619.5])
row.add_row(["Sydney", 2058, 4336374, 1214.8])
row.add_row(["Melbourne", 1566, 3806092, 646.9])
row.add_row(["Perth", 5386, 1554769, 869.4])
print row
+-----------+------+------------+-----------------+ | City name | Area | Population | Annual Rainfall | +-----------+------+------------+-----------------+ | Adelaide | 1295 | 1158259 | 600.5 | | Brisbane | 5905 | 1857594 | 1146.4 | | Darwin | 112 | 120900 | 1714.7 | | Hobart | 1357 | 205556 | 619.5 | | Sydney | 2058 | 4336374 | 1214.8 | | Melbourne | 1566 | 3806092 | 646.9 | | Perth | 5386 | 1554769 | 869.4 | +-----------+------+------------+-----------------+
# 通过列添加
col = PrettyTable()
col.add_column("City name",["Adelaide","Brisbane","Darwin","Hobart","Sydney","Melbourne","Perth"],align="l",valign="t")
col.add_column("Area", [1295, 5905, 112, 1357, 2058, 1566, 5386])
col.add_column("Population", [1158259, 1857594, 120900, 205556, 4336374, 3806092, 1554769])
col.add_column("Annual Rainfall",[600.5, 1146.4, 1714.7, 619.5, 1214.8, 646.9, 869.4])
print col
+-----------+------+------------+-----------------+ | City name | Area | Population | Annual Rainfall | +-----------+------+------------+-----------------+ | Adelaide | 1295 | 1158259 | 600.5 | | Brisbane | 5905 | 1857594 | 1146.4 | | Darwin | 112 | 120900 | 1714.7 | | Hobart | 1357 | 205556 | 619.5 | | Sydney | 2058 | 4336374 | 1214.8 | | Melbourne | 1566 | 3806092 | 646.9 | | Perth | 5386 | 1554769 | 869.4 | +-----------+------+------------+-----------------+
# 行列混输
mix = PrettyTable()
mix.field_names = ["City name", "Area"]
mix.add_row(["Adelaide",1295])
mix.add_row(["Brisbane",5905])
mix.add_row(["Darwin", 112])
mix.add_row(["Hobart", 1357])
mix.add_row(["Sydney", 2058])
mix.add_row(["Melbourne", 1566])
mix.add_row(["Perth", 5386])
mix.add_column("Population", [1158259, 1857594, 120900, 205556, 4336374, 3806092, 1554769])
mix.add_column("Annual Rainfall",[600.5, 1146.4, 1714.7, 619.5, 1214.8, 646.9, 869.4])
print mix
+-----------+------+------------+-----------------+ | City name | Area | Population | Annual Rainfall | +-----------+------+------------+-----------------+ | Adelaide | 1295 | 1158259 | 600.5 | | Brisbane | 5905 | 1857594 | 1146.4 | | Darwin | 112 | 120900 | 1714.7 | | Hobart | 1357 | 205556 | 619.5 | | Sydney | 2058 | 4336374 | 1214.8 | | Melbourne | 1566 | 3806092 | 646.9 | | Perth | 5386 | 1554769 | 869.4 | +-----------+------+------------+-----------------+
# 清空行内容
mix.clear_rows()
print mix
+-----------+------+------------+-----------------+ | City name | Area | Population | Annual Rainfall | +-----------+------+------------+-----------------+ +-----------+------+------------+-----------------+
# 清空内容
mix.clear()
print mix
++ || ++ ++
# 加入转义字符
t = PrettyTable(['Field 1', 'Field 2'])
t.add_row(['value 1', 'value2\nsecond line'])
t.add_row(['value 3\n\nother line', 'value4\n\n\nvalue5'])
print t.get_string(hrules=ALL)
+------------+-------------+ | Field 1 | Field 2 | +------------+-------------+ | value 1 | value2 | | | second line | +------------+-------------+ | value 3 | value4 | | | | | other line | | | | value5 | +------------+-------------+
# 测试多种语言的支持情况
x = PrettyTable(["Kanji", "Hiragana", "English"])
x.add_row(["神戸", "こうべ", "Kobe"])
x.add_row(["京都", "きょうと", "Kyoto"])
x.add_row(["長崎", "ながさき", "Nagasaki"])
x.add_row(["名古屋", "なごや", "Nagoya"])
x.add_row(["大阪", "おおさか", "Osaka"])
x.add_row(["札幌", "さっぽろ", "Sapporo"])
x.add_row(["東京", "とうきょう", "Tokyo"])
x.add_row(["横浜", "よこはま", "Yokohama"])
print x
+--------+------------+----------+ | Kanji | Hiragana | English | +--------+------------+----------+ | 神戸 | こうべ | Kobe | | 京都 | きょうと | Kyoto | | 長崎 | ながさき | Nagasaki | | 名古屋 | なごや | Nagoya | | 大阪 | おおさか | Osaka | | 札幌 | さっぽろ | Sapporo | | 東京 | とうきょう | Tokyo | | 横浜 | よこはま | Yokohama | +--------+------------+----------+
本社区仅针对特定人员开放
查看需注册登录并通过风险意识测评
5秒后跳转登录页面...
移动端课程