您好,登錄后才能下訂單哦!
這篇文章主要介紹“Python怎么利用prettytable實現格式化輸出內容”,在日常操作中,相信很多人在Python怎么利用prettytable實現格式化輸出內容問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Python怎么利用prettytable實現格式化輸出內容”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
我們用 MySQL 客戶端查詢數據的時候,是以下面這種格式顯示的:
內容展示的非常漂亮,而 Python 有一個第三方模塊叫 prettytable,專門用來將數據以上面這種格式輸出,我們來看一下用法。
類似于數據庫中的表,由表頭(或者說字段名),以及每一行的內容組成。
from prettytable import PrettyTable # 傳入的 name、age、country 相當于表頭 tb = PrettyTable(["name", "age", "country"]) # 調用 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"]) # 調用 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 | +----------------+-----+-------------+--------+ """
輸出內容是不是和 MySQL數據庫類似呢。
此外 prettytable 還支持從 csv、數據庫、html 等數據源中導入數據,但說實話,從數據源讀取數據我們一般使用 pandas,并且還會伴隨著數據處理。而使用 prettytable 只是為了讓程序中產生的信息,能夠以結構化的形式打印,很少會從文件或數據庫里面讀數據。
我們也可以輸出 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 表示開始和結束的行數(從 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 > ~^^^^^^^^^^^^^^^^~^^^^^~^^^^^^^^^^^^^~^^^^^^^^~ """
到此,關于“Python怎么利用prettytable實現格式化輸出內容”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。