PrettyTable介绍与基本使用(二)
接上次的PrettyTable
使用介绍,继续介绍PrettyTable
的用法。
表格对齐方式
使用align
属性,配置表格数据的对齐方式,"l"
,"r"
,"c"
分别代表左对齐,右对齐,居中对齐, 默认情况下,均使用居中对齐。对整个表的设置使用:
x.align = "r"
对某一列进行设置,只需要提供对应的字段名, 默认情况下,均使用居中对齐。:
x.align["City name"] = "l"
x.align["Area"] = "c"
x.align["Population"] = "r"
x.align["Annual Rainfall"] = "c"
print(x)
表格排序
PrettyTable
可以对表格进行排序,类似数据库的排序。但功能还是有限。只要设置get_string
函数中的参数sortby
。默认使用的是升序。可以通过设置reversesort=True
来实现降序。
print(x.get_string(sortby="Population"))
print(x.get_string(sortby="Population",reversesort=True))
以上排序只是在输出的时候进行排序,而不会对原始表格排序,如果希望改变表格的排序方式,可以使用表格的属性sortby
。
from prettytable import PrettyTable
x = PrettyTable()
x.title = 'Table 1 City Info'
x.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
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.get_string(sortby="Population",reversesort=True))
print(x)
x.sortby = "Population"
print(x)
当然,可以设置x.reversesort = True
实现降序。通过设置x.sortby = None
取消表格的排序。
表格样式
PrettyTable
使用set_style
设置表格样式,默认定义了几种样式(使用时候需要从prettytable
中导入):
- MSWORD_FRIENDLY
- DEFAULT
- MARKDOWN
- ORGMODE
- DOUBLE_BORDER
from prettytable import PrettyTable
from prettytable import MSWORD_FRIENDLY,DEFAULT,PLAIN_COLUMNS,MARKDOWN,ORGMODE,DOUBLE_BORDER
x = PrettyTable()
x.title = 'Table 1 City Info'
x.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
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])
x.set_style(MSWORD_FRIENDLY)
print(x)
x.set_style(DEFAULT)
print(x)
x.set_style(PLAIN_COLUMNS)
print(x)
x.set_style(MARKDOWN)
print(x)
x.set_style(ORGMODE)
print(x)
x.set_style(DOUBLE_BORDER)
print(x)
自定义表格样式
除了上面说的默认定义好的表格样式,我们也可以自定义表格样式。PrettyTable
提供了一系列表格样式的选项,供我们设置。
- border - A boolean option (must be True or False). Controls whether or not a border is drawn around the table.
- header - A boolean option (must be True or False). Controls whether or not the first row of the table is a header showing the names of all the fields.
- hrules - Controls printing of horizontal rules after rows. Allowed values: FRAME, HEADER, ALL, NONE - note that these are variables defined inside the prettytable module so make sure you import them or use prettytable.FRAME etc.
- vrules - Controls printing of vertical rules between columns. Allowed values: FRAME, ALL, NONE.
- int_format - A string which controls the way integer data is printed. This works like: print(“%<int_format>d” % data)
- float_format - A string which controls the way floating point data is printed. This works like: print(“%<float_format>f” % data)
- padding_width - Number of spaces on either side of column data (only used if left and right paddings are None).
- left_padding_width - Number of spaces on left hand side of column data.
- right_padding_width - Number of spaces on right hand side of column data.
- vertical_char - Single character string used to draw vertical lines. Default is |.
- horizontal_char - Single character string used to draw horizontal lines. Default is -.
- junction_char - Single character string used to draw line junctions. Default is +.
- top_junction_char - single character string used to draw top line junctions. Default is junction_char.
- bottom_junction_char - single character string used to draw bottom line junctions. Default is junction_char.
- right_junction_char - single character string used to draw right line junctions. Default is junction_char.
- left_junction_char - single character string used to draw left line junctions. Default is junction_char.
- top_right_junction_char - single character string used to draw top-right line junctions. Default is junction_char.
- top_left_junction_char - single character string used to draw top-left line junctions. Default is junction_char.
- bottom_right_junction_char - single character string used to draw bottom-right line junctions. Default is junction_char
- bottom_left_junction_char - single character string used to draw bottom-left line junctions. Default is junction_char.
和sortby
类似,可以仅仅设置一次样式(不改变原来的表,通过get_string
函数),可以设置长久的样式(直接对原表设置)。
长久设置
x = PrettyTable()
x.border = False
x.header = False
x.padding_width = 5
x = PrettyTable(border=False, header=False, padding_width=5)
设置一次
print(x)
print(x.get_string(border=False))
print(x)
Json数据格式导出
PrettyTable
支持Json
数据导出,使用get_json_string()
。返回一个列表,列表第一项为列字段名,其后的每一个json
对象对应一行数据,键对应字段名,值对应数据。并且按照字母顺序排序好了。
from prettytable import PrettyTable
from prettytable import MSWORD_FRIENDLY,DEFAULT
x = PrettyTable()
x.title = 'Table 1 City Info'
x.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
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.get_json_string())
HTML数据格式导出
PrettyTable
支持HTML
数据导出,使用get_html_string()
。和get_string()
类似,get_html_string()
支持fields
, start
, end
, sortby
和 reversesort
参数。
print(x.get_html_string())
可以传递一个参数临时设置打印的格式,此格式包含了css
样式。当然也可以永久设置。
print(x.get_html_string(format=True))
x.format = True
甚至我们可以直接设置表格的HTML
属性。注意这个HTML
属性设置只是针对table
标签。只需要给get_html_string
的attributes
属性传递一个字典,字典的键对应HTML
的属性,字典的值对应HTML
的属性值。并且,传递的字典的键必须是HTML
合法的属性。例如name
,class
。
print(x.get_html_string(attributes={"name":"my_table", "class":"red_table"}))
截取表格
可以从原来的表格中取出部分数据,生成新的表格,这个python
的列表是类似的。
from prettytable import PrettyTable
from prettytable import MSWORD_FRIENDLY,DEFAULT
x = PrettyTable()
x.title = 'Table 1 City Info'
x.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
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)
new_table = x[0:5]
print(new_table)