PrettyTable安裝(推薦學(xué)習(xí):Python視頻教程)
pip install 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]) print(tb)
輸出:
+-----------+------+------------+-----------------+ | 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 | +-----------+------+------------+-----------------+
如果沒有添加表頭,那么會以默認(rèn)的Field+編號顯示,例如:
+---------+----------+----------+------------+ | Field 1 | Field 2 | Field 3 | Field 4 | +---------+----------+----------+------------+
更多Python相關(guān)技術(shù)文章,請?jiān)L問Python教程欄目進(jìn)行學(xué)習(xí)!
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com