[僕] text な table を作る

僕ト云フ事

たろマークはてなブックマーク

2011年10月18日

[pypi][python][texttable] text な table を作る

そのまま、texttable っていうのが PyPI にある。
pip install textable して、こんなコードを書くと


# -*- coding: utf-8 -*-
 
from texttable import Texttable
 
table = Texttable()
 
table.add_rows([
        ['id', 'name', 'content'],
        [1, 'aaaaa bbbbb', 'hoge'],
        [2, 'bbb cccc', 'fuga'],
        [3, 'dddd eeee', 'awawa'],
        ])
print table.draw()

こんな感じで出力できる。

 $ python text_table.py
 +----+-------------+---------+
 | id |    name     | content |
 +====+=============+=========+
 | 1  | aaaaa bbbbb | hoge    |
 +----+-------------+---------+
 | 2  | bbb cccc    | fuga    |
 +----+-------------+---------+
 | 3  | dddd eeee   | awawa   |
 +----+-------------+---------+

add_rows() はまとめて行を追加、1行ごとであれば add_row() ってメソッドもある。

set_cols_align() で、各 column の内容の右寄せ/左寄せ/センタリングしたり、
set_cols_valign() で縦方向の位置決め、set_deco() で罫線の style 変えたりできるっぽい。

textable documentation

blog comments powered by Disqus