Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

3.3. texttable - module for creating simple ASCII tables

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    |
+--------+-----+-----------+		
		
		

3.3.1. 对齐设置

set_header_align(self, array) 设置水平对齐

  • l 表示左对齐
  • r 表示右对齐
  • c 表示居中对齐

set_cols_align(self, array) 设置水平对齐

  • l 表示左对齐
  • r 表示右对齐
  • c 表示居中对齐

set_cols_valign(self, array) 设置垂直对齐

  • t 表示顶部对齐
  • m 表示中间对齐
  • b 表示底部对齐
			
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   |
+----------+-----+----------+			
			
			

3.3.2. 设置表格风格

			
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   			
			
			

3.3.3. 自定义风格

自定义行列线条字符

			
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) 数字的四个参数分别是:

  • horizontal 水平画线字符
  • vertical 垂直画线字符
  • corner 转角画线字符
  • header 表头画线字符

默认是 ['-', '|', '+', '=']

下面这段代码模仿 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 命令中输出结果一致。

3.3.4. 设置列数据类型

			
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			
			
			

3.3.5. 彩色表格

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())