Python - prettytable


目錄

about

prettytable模塊可以用來美化輸出。

install

pip install prettytable
pip install -i https://pypi.doubanio.com/simple prettytable==2.0.0

usage

基本用法

import prettytable
from faker import Faker   # pip install Faker

fk = Faker(locale='zh_CN')

table = prettytable.PrettyTable(['序號', '姓名', '密碼', '地址'])
for i in range(1, 6):
    table.add_row([i, fk.name(), fk.password(special_chars=False), fk.address()])
print(table)
"""
+------+--------+------------+--------------------------------------+
| 序號 |  姓名  |    密碼    |                 地址                 |
+------+--------+------------+--------------------------------------+
|  1   | 朱淑華 | QClSmJFt6L |   貴州省濟南縣江北太原路E座 229737   |
|  2   |  周寧  | ehB7SvDhf6 |    北京市潔縣清城荊門街F座 350378    |
|  3   | 廖玉英 | 2WObppm07t |    天津市秀梅市璧山馬路G座 873881    |
|  4   |  傅軍  | D5TpvZxh2U | 天津市哈爾濱市璧山石家庄路j座 282678 |
|  5   | 劉丹丹 | blGHSKNj5M |    福建省想市普陀濟南街U座 959990    |
+------+--------+------------+--------------------------------------+
"""

freestyle

import prettytable
from faker import Faker   # pip install Faker

fk = Faker(locale='zh_CN')

table = prettytable.PrettyTable(['序號', '姓名', '密碼', '地址'])
# table.set_style(prettytable.DEFAULT)
# table.set_style(prettytable.ORGMODE)  # 跟默認差不多
# table.set_style(prettytable.MARKDOWN)   # markdown樣式
# table.set_style(prettytable.PLAIN_COLUMNS)   # 無邊框
# table.set_style(prettytable.MSWORD_FRIENDLY)   # 無橫線
table.set_style(prettytable.RANDOM)   # 隨機
for i in range(1, 6):
    table.add_row([i, fk.name(), fk.password(special_chars=False), fk.address()])
print(table)

除了上述的幾種樣式,也可以自定義表格樣式:

  • 設定左對齊: tb.align = "l"
  • 設定數字輸出格式: tb.float_format = "2.2"
  • 設定邊框連接符為*: tb.junction_char = "*"
  • 設定左側不填充空白字符: tb.left_padding_width = 0

將結果輸出

import prettytable
from faker import Faker   # pip install Faker


fk = Faker(locale='zh_CN')

table = prettytable.PrettyTable(['序號', '姓名', '密碼', '地址'])
for i in range(1, 6):
    table.add_row([i, fk.name(), fk.password(special_chars=False), fk.address()])

# 輸出為 html
# html = table.get_html_string()
# print(html)

# 輸出為json
# json_string = table.get_json_string()
# print(json_string)

# 輸出為CSV
# csv = table.get_csv_string()
# print(csv)

# 輸出為字符串
# s = table.get_string()
# print(s)

自動根據title列表長度進行表格展示
之前遇到一個需求,就是根據sql語句自動輸出結果列數,比如select * from table,展示所有的匹配字段;當select id,name from table時,只輸出idname這兩列:

from prettytable import PrettyTable



data_list = [
    {'id': '1', 'name': 'zhangkai', 'age': '22', 'phone': '13651054608', 'dep': 'IT', 'dt': '2013-04-01'},
    {'id': '2', 'name': 'likai', 'age': '28', 'phone': '13451024608', 'dep': 'HR', 'dt': '2015-01-07'},
    {'id': '3', 'name': 'wangkai', 'age': '21', 'phone': '13451054608', 'dep': 'IT', 'dt': '2017-04-01'},
    {'id': '4', 'name': 'zhaokai', 'age': '44', 'phone': '15653354208', 'dep': 'Sales', 'dt': '2016-02-01'},
    {'id': '5', 'name': 'sunkuai', 'age': '23', 'phone': '13351024606', 'dep': 'IT', 'dt': '2013-03-16'}
]

# select * from table;
title1 = ['id', 'name', 'age', 'phone', 'dep', 'dt']
# select id,name,age from table;
title2 = ['id', 'name', 'age']

table = PrettyTable(title2)

for i in data_list:
    table.add_row([i[j] for j in title2])   # 只展示title列表中的字段

print(table)
"""
+----+----------+-----+
| id |   name   | age |
+----+----------+-----+
| 1  | zhangkai |  22 |
| 2  |  likai   |  28 |
| 3  | wangkai  |  21 |
| 4  | zhaokai  |  44 |
| 5  | sunkuai  |  23 |
+----+----------+-----+
"""

that's all, see also:

Python3表格美化模塊prettytable


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM