返回介绍

12.3. texttable - module for creating simple ASCII tables

发布于 2024-02-10 15:26:30 字数 7154 浏览 0 评论 0 收藏 0

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

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

12.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   			

12.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 命令中输出结果一致。

12.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			

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文