您好,登錄后才能下訂單哦!
這篇文章主要介紹“Python怎么利用prettytable實現(xiàn)格式化輸出內(nèi)容”,在日常操作中,相信很多人在Python怎么利用prettytable實現(xiàn)格式化輸出內(nèi)容問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Python怎么利用prettytable實現(xiàn)格式化輸出內(nèi)容”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
我們用 MySQL 客戶端查詢數(shù)據(jù)的時候,是以下面這種格式顯示的:
內(nèi)容展示的非常漂亮,而 Python 有一個第三方模塊叫 prettytable,專門用來將數(shù)據(jù)以上面這種格式輸出,我們來看一下用法。
類似于數(shù)據(jù)庫中的表,由表頭(或者說字段名),以及每一行的內(nèi)容組成。
from prettytable import PrettyTable # 傳入的 name、age、country 相當(dāng)于表頭 tb = PrettyTable(["name", "age", "country"]) # 調(diào)用 add_row 添加行記錄 tb.add_row(["Jack Morrison", 49, "America"]) tb.add_row(["Shimada Genji", 35, "Japan"]) tb.add_row(["Shimada Hanzo", 38, "Japan"]) tb.add_row(["Angela Ziegler", 37, "Switzerland"]) print(tb) """ +----------------+-----+-------------+ | name | age | country | +----------------+-----+-------------+ | Jack Morrison | 49 | America | | Shimada Genji | 35 | Japan | | Shimada Hanzo | 38 | Japan | | Angela Ziegler | 37 | Switzerland | +----------------+-----+-------------+ """
如果在編寫的過程中,我們需要臨時添加一列,prettytable 也是支持的。
from prettytable import PrettyTable tb = PrettyTable(["name", "age", "country"]) tb.add_row(["Jack Morrison", 49, "America"]) tb.add_row(["Shimada Genji", 35, "Japan"]) tb.add_row(["Shimada Hanzo", 38, "Japan"]) tb.add_row(["Angela Ziegler", 37, "Switzerland"]) # 調(diào)用 add_column 添加一列 tb.add_column("gender", ["male", "male", "male", "female"]) print(tb) """ +----------------+-----+-------------+--------+ | name | age | country | gender | +----------------+-----+-------------+--------+ | Jack Morrison | 49 | America | male | | Shimada Genji | 35 | Japan | male | | Shimada Hanzo | 38 | Japan | male | | Angela Ziegler | 37 | Switzerland | female | +----------------+-----+-------------+--------+ """
輸出內(nèi)容是不是和 MySQL數(shù)據(jù)庫類似呢。
此外 prettytable 還支持從 csv、數(shù)據(jù)庫、html 等數(shù)據(jù)源中導(dǎo)入數(shù)據(jù),但說實話,從數(shù)據(jù)源讀取數(shù)據(jù)我們一般使用 pandas,并且還會伴隨著數(shù)據(jù)處理。而使用 prettytable 只是為了讓程序中產(chǎn)生的信息,能夠以結(jié)構(gòu)化的形式打印,很少會從文件或數(shù)據(jù)庫里面讀數(shù)據(jù)。
我們也可以輸出 table 的指定行、指定列。
from prettytable import PrettyTable tb = PrettyTable(["name", "age", "country", "gender"]) tb.add_row(["Jack Morrison", 49, "America", "male"]) tb.add_row(["Shimada Genji", 35, "Japan", "male"]) tb.add_row(["Shimada Hanzo", 38, "Japan", "male"]) tb.add_row(["Angela Ziegler", 37, "Switzerland", "female"]) # 只輸出 name 和 age 兩列 # start 和 end 表示開始和結(jié)束的行數(shù)(從 0 開始) print(tb.get_string(fields=["name", "age"], start=1, end=3)) """ +---------------+-----+ | name | age | +---------------+-----+ | Shimada Genji | 35 | | Shimada Hanzo | 38 | +---------------+-----+ """ # 此外可以用來進行排序 print(tb.get_string(sortby="age", reversesort=True)) """ +----------------+-----+-------------+--------+ | name | age | country | gender | +----------------+-----+-------------+--------+ | Jack Morrison | 49 | America | male | | Shimada Hanzo | 38 | Japan | male | | Angela Ziegler | 37 | Switzerland | female | | Shimada Genji | 35 | Japan | male | +----------------+-----+-------------+--------+ """
表格也支持幾種不同的樣式,供我們選擇。
from prettytable import * tb = PrettyTable(["name", "age", "country", "gender"]) tb.add_row(["Jack Morrison", 49, "America", "male"]) tb.add_row(["Shimada Genji", 35, "Japan", "male"]) tb.add_row(["Shimada Hanzo", 38, "Japan", "male"]) tb.add_row(["Angela Ziegler", 37, "Switzerland", "female"]) # 樣式支持以下幾種: # DEFAULT、MSWORD_FRIENDLY、PLAIN_COLUMNS、RANDOM # 之前的樣式就是 DEFAULT tb.set_style(MSWORD_FRIENDLY) print(tb) """ | name | age | country | gender | | Jack Morrison | 49 | America | male | | Shimada Genji | 35 | Japan | male | | Shimada Hanzo | 38 | Japan | male | | Angela Ziegler | 37 | Switzerland | female | """ tb.set_style(PLAIN_COLUMNS) print(tb) """ name age country gender Jack Morrison 49 America male Shimada Genji 35 Japan male Shimada Hanzo 38 Japan male Angela Ziegler 37 Switzerland female """ tb.set_style(RANDOM) print(tb) """ ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++; . name age country gender . ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++; . Jack Morrison 49 America male . . Shimada Genji 35 Japan male . . Shimada Hanzo 38 Japan male . . Angela Ziegler 37 Switzerland female . ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++; """
說實話,還是默認的 DEFAULT 樣式最好看。
from prettytable import * tb = PrettyTable(["name", "age", "country", "gender"]) tb.add_row(["Jack Morrison", 49, "America", "male"]) tb.add_row(["Shimada Genji", 35, "Japan", "male"]) tb.add_row(["Shimada Hanzo", 38, "Japan", "male"]) tb.add_row(["Angela Ziegler", 37, "Switzerland", "female"]) # l 代表左對齊,c 代表居中,r 代表右對齊 # 默認居中 tb.align["name"] = "l" tb.align["age"] = "c" tb.align["country"] = "r" print(tb) """ +----------------+-----+-------------+--------+ | name | age | country | gender | +----------------+-----+-------------+--------+ | Jack Morrison | 49 | America | male | | Shimada Genji | 35 | Japan | male | | Shimada Hanzo | 38 | Japan | male | | Angela Ziegler | 37 | Switzerland | female | +----------------+-----+-------------+--------+ """
在 PrettyTable 中,邊框由三個部分組成:橫邊框,豎邊框,和邊框連接符,我們都可以修改。
from prettytable import * tb = PrettyTable(["name", "age", "country", "gender"]) tb.add_row(["Jack Morrison", 49, "America", "male"]) tb.add_row(["Shimada Genji", 35, "Japan", "male"]) tb.add_row(["Shimada Hanzo", 38, "Japan", "male"]) tb.add_row(["Angela Ziegler", 37, "Switzerland", "female"]) # 是否顯示邊框,默認為True tb.border = True # 橫邊框 tb.horizontal_char = '^' # 豎邊框 tb.vertical_char = '>' # 邊框連接符 tb.junction_char='~' print(tb) """ ~^^^^^^^^^^^^^^^^~^^^^^~^^^^^^^^^^^^^~^^^^^^^^~ > name > age > country > gender > ~^^^^^^^^^^^^^^^^~^^^^^~^^^^^^^^^^^^^~^^^^^^^^~ > Jack Morrison > 49 > America > male > > Shimada Genji > 35 > Japan > male > > Shimada Hanzo > 38 > Japan > male > > Angela Ziegler > 37 > Switzerland > female > ~^^^^^^^^^^^^^^^^~^^^^^~^^^^^^^^^^^^^~^^^^^^^^~ """
到此,關(guān)于“Python怎么利用prettytable實現(xiàn)格式化輸出內(nèi)容”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。