prettytable
印刷フォームの場合
import prettytable as pt
# tb = pt.PrettyTable( ["City name", "Area", "Population", "Annual Rainfall"])
tb = pt.PrettyTable()
tb.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
tb.add_row(["Adelaide",1295, 1158259, 600.5])
tb.add_row(["Brisbane",5905, 1857594, 1146.4])
tb.add_row(["Darwin", 112, 120900, 1714.7])
tb.add_row(["Hobart", 1357, 205556,619.5])
tb.add_column('index',[1,2,3,4]) #* 添加一列 *
print(tb)
結果:
+-----------+------+------------+-----------------+-------+
| City name | Area | Population | Annual Rainfall | index |
+-----------+------+------------+-----------------+-------+
| Adelaide | 1295 | 1158259 | 600.5 | 1 |
| Brisbane | 5905 | 1857594 | 1146.4 | 2 |
| Darwin | 112 | 120900 | 1714.7 | 3 |
| Hobart | 1357 | 205556 | 619.5 | 4 |
+-----------+------+------------+-----------------+-------+
- add_row():ラインデータに加え、データが追加されなければならない、もしリスト(リスト)、およびリストヘッダ長と同じにデータの長さ。
- ADD_COLUMN():データ列を追加します。
csvファイルからデータを追加します
import prettytable as pt
from prettytable import from_csv
table = pt.PrettyTable()
with open('res.csv', 'r') as f:
table = from_csv(fp)
print(table)
アラインメント
align
ユーザーによる配向モードのセットを提供する、値は l,r,c
簡単代表 左对齐,右对齐和居中
設定されていない場合は、デフォルトの中央揃え。
tb.align["City name"] = "l"
tb.align["Area"] = "c"
tb.align["Population"] = "r"
tb.align["Annual Rainfall"] = "c"
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Adelaide | 1295 | 1158259 | 600.5 |
| Brisbane | 5905 | 1857594 | 1146.4 |
| Darwin | 112 | 120900 | 1714.7 |
| Hobart | 1357 | 205556 | 619.5 |
+-----------+------+------------+-----------------+
詳細なアプリケーションリファレンス:PythonのPrettytableの印刷フォーム