PrettyTable介绍
Python通过prettytable模块可以将输出内容如表格方式整齐的输出。
使用该模块,用户可以在日志中输出清晰的日志文档。
简单使用
from prettytable import PrettyTable x = PrettyTable(["page","url","热度",'title']) x.align["title"] = "1" # Left align city names x.padding_width = 1 x.add_row(['12','http://xxx','100','title']) print(x)
常用方法如下:
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"""
示例如下:
In [1]:
from prettytable import *
In [75]:
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)
In [76]:
# 通过行添加 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)
In [77]:
# 通过列添加 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)
In [87]:
# 行列混输 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)
In [81]:
# 清空内容 mix.clear() print(mix)
In [88]:
# 清空行内容 mix.clear_rows() print(mix)
In [13]:
# 加入转义字符 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))
In [14]:
# 测试多种语言的支持情况 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)