知乎专栏 | 多维度架构 |
https://github.com/foutaise/texttable/
pip install texttable
程序演示
from texttable import Texttable table = Texttable() table.add_rows([["Name", "Age", "Nickname"], ["Neo", 35, "netkiller"], ["李磊", 23, "Lee"], ["韩美美", 28, "May"]]) print(table.draw())
+--------+-----+-----------+ | Name | Age | Nickname | +========+=====+===========+ | Neo | 35 | netkiller | +--------+-----+-----------+ | 李磊 | 23 | Lee | +--------+-----+-----------+ | 韩美美 | 28 | May | +--------+-----+-----------+
set_header_align(self, array) 设置水平对齐
set_cols_align(self, array) 设置水平对齐
set_cols_valign(self, array) 设置垂直对齐
from texttable import Texttable table = Texttable() table.set_cols_align(["l", "r", "c"]) table.set_cols_valign(["t", "m", "b"]) table.add_rows([["Name", "Age", "Nickname"], ["Mr\nXavier\nHuon", 32, "Xav'"], ["Mr\nBaptiste\nClement", 1, "Baby"], ["Mme\nLouise\nBourgeau", 28, "Lou\n\nLoue"]]) print(table.draw()) print()
输出结果
+----------+-----+----------+ | Name | Age | Nickname | +==========+=====+==========+ | Mr | | | | Xavier | 32 | | | Huon | | Xav' | +----------+-----+----------+ | Mr | | | | Baptiste | 1 | | | Clement | | Baby | +----------+-----+----------+ | Mme | | Lou | | Louise | 28 | | | Bourgeau | | Loue | +----------+-----+----------+
from texttable import Texttable table = Texttable() for header in (Texttable.BORDER, Texttable.HEADER, Texttable.HLINES, Texttable.VLINES): table.set_deco(header) table.set_cols_align(["l", "r", "c"]) table.set_cols_valign(["t", "m", "b"]) table.add_rows([["Name", "Age", "Nickname"], ["Neo", 35, "netkiller"], ["李磊", 23, "Lee"], ["韩美美", 28, "May"]]) print(table.draw()) print("\n\n")
输出结果
+--------------------------+ | Name Age Nickname | | Neo 35 netkiller | | 李磊 23 Lee | | 韩美美 28 May | +--------------------------+ Name Age Nickname ======================== Neo 35 netkiller 李磊 23 Lee 韩美美 28 May Neo 35 netkiller 李磊 23 Lee 韩美美 28 May Name Age Nickname Neo 35 netkiller +--------------------------+ 李磊 23 Lee +--------------------------+ 韩美美 28 May +--------------------------+ Neo 35 netkiller +--------------------------+ 李磊 23 Lee +--------------------------+ 韩美美 28 May +--------------------------+ Neo 35 netkiller +--------------------------+ 李磊 23 Lee +--------------------------+ 韩美美 28 May Name | Age | Nickname Neo | 35 | netkiller 李磊 | 23 | Lee 韩美美 | 28 | May Neo | 35 | netkiller 李磊 | 23 | Lee 韩美美 | 28 | May Neo | 35 | netkiller 李磊 | 23 | Lee 韩美美 | 28 | May Neo | 35 | netkiller 李磊 | 23 | Lee 韩美美 | 28 | May
自定义行列线条字符
set_chars(self, array) | Set the characters used to draw lines between rows and columns | | - the array should contain 4 fields: | | [horizontal, vertical, corner, header] | | - default is set to: | | ['-', '|', '+', '=']
set_chars(self, array) 数字的四个参数分别是:
默认是 ['-', '|', '+', '=']
下面这段代码模仿 MySQL 终端输出样式
table = Texttable() table.set_cols_align(["r", "l", "c", "l", "l"]) table.set_cols_valign(["m", "m", "m", "m", "m"]) table.set_chars(['-', '|', '+', '-']) table.set_cols_dtype(['i', 't', 'i', 't', 'a']) table.add_rows([["id", "name", "age", "nickname", "ctime"], [1, "Neo", 35, "netkiller", "2021-05-16 10:14:00"], [2, "Tom", 23, "Lee", "2021-05-16 10:14:00"], [3, "Jerry", 28, "May", "2021-05-16 10:14:00"]]) print(table.draw()) print()
+----+-------+-----+-----------+---------------------+ | id | name | age | nickname | ctime | +----+-------+-----+-----------+---------------------+ | 1 | Neo | 35 | netkiller | 2021-05-16 10:14:00 | +----+-------+-----+-----------+---------------------+ | 2 | Tom | 23 | Lee | 2021-05-16 10:14:00 | +----+-------+-----+-----------+---------------------+ | 3 | Jerry | 28 | May | 2021-05-16 10:14:00 | +----+-------+-----+-----------+---------------------+
怎么样,似曾相识吧?跟 mysql 命令中输出结果一致。
from texttable import Texttable table = Texttable() table.set_deco(Texttable.HEADER) table.set_cols_dtype(['t', # text 'f', # float (decimal) 'e', # float (exponent) 'i', # integer 'a']) # automatic table.set_cols_align(["l", "r", "r", "r", "l"]) table.add_rows([["text", "float", "exp", "int", "auto"], ["abcd", "67", 654, 89, 128.001], ["efghijk", 67.5434, .654, 89.6, 12800000000000000000000.00023], ["lmn", 5e-78, 5e-78, 89.4, .000000000000128], ["opqrstu", .023, 5e+78, 92., 12800000000000000000000]]) print(table.draw())
输出结果
text float exp int auto ============================================== abcd 67.000 6.540e+02 89 128.001 efghijk 67.543 6.540e-01 90 1.280e+22 lmn 0.000 5.000e-78 89 0.000 opqrstu 0.023 5.000e+78 92 1.280e+22
texttable 本身不支持 ANSI 彩色文本输出,我以修复了该 Bug,已经想修复代码pull request 给作者。
Pull Request: https://github.com/foutaise/texttable/pull/75
我的代码库地址: https://github.com/netkiller/texttable
from texttable import Texttable from colorama import Fore, Back, Style, init table = Texttable() table.set_chars(['-', '|', '+', '-']) # table.set_cols_width([8, 5, 19]) table.add_rows([["Name", "Age", "Nickname"], ["Neo", 35, Fore.RED+"netkiller"+Fore.RESET], ["李磊", 23, Fore.GREEN+"Lee"+Fore.RESET], ["韩美美", 28, Fore.BLUE+"May"+Fore.RESET]]) print(table.draw())