python prettytable模塊


簡介

Python通過PrettyTable模塊可以將輸出內容如表格方式整齊地輸出。

安裝

pip install prettytable
 
 
 
         
  • 1

示例

from prettytable import PrettyTable
table = PrettyTable(["animal", "ferocity"])
table.add_row(["wolverine", 100])
table.add_row(["grizzly", 87])
table.add_row(["Rabbit of Caerbannog", 110])
table.add_row(["cat", -1])
table.add_row(["platypus", 23])
table.add_row(["dolphin", 63])
table.add_row(["albatross", 44])
table.sort_key("ferocity")
table.reversesort = True
print(table)

'''效果圖 +----------------------+----------+ | animal | ferocity | +----------------------+----------+ | Rabbit of Caerbannog | 110 | | wolverine | 100 | | grizzly | 87 | | dolphin | 63 | | albatross | 44 | | platypus | 23 | | cat | -1 | +----------------------+----------+ '''
 
 
 
         
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

使用

創建表

直接創建

pt = PrettyTable()
 
 
 
         
  • 1

從已有文件創建

CSV
from prettytable import from_csv 
fp = open("mytable.csv", "r") 
pt = from_csv(fp) 
fp.close()
 
 
 
         
  • 1
  • 2
  • 3
  • 4
HTML
from prettytable import from_html 
pts = from_html(html_string)
 
 
 
         
  • 1
  • 2
SQL
from prettytable import from_db_cursor 
db_cur.execute("SELECT * FROM mytable") 
pt = from_db_cursor(db_cur)
 
 
 
         
  • 1
  • 2
  • 3

添加元素

按行添加

pt.add_row()
 
 
 
         
  • 1

按列添加

pt.add_column()
 
 
 
         
  • 1

輸出格式

ASCII碼表

直接輸出
print(pt)
 
 
 
         
  • 1
無表格框輸出
print(pt.get_string())
 
 
 
         
  • 1

HTML表

print(pt.get_html_string())
 
 
 
         
  • 1

選擇子表

print(pt.get_string(fields = ["City name", "Population"]))
 
 
 
         
  • 1
#輸出前4列
print(pt.get_string(start = 0, end = 3))
 
 
 
         
  • 1
  • 2
new_table = old_table[0:3]
print(new_table)
 
 
 
         
  • 1
  • 2

表排序

print x.get_string(sortby="Annual Rainfall", reversesort=True)
 
 
 
         
  • 1

控制表樣式

自帶樣式

#參數還可以選擇“DEFAULT”、“PLAIN_COLUMNS”
from prettytable import MSWORD_FRIENDLY
x.set_style(MSWORD_FRIENDLY) 
print(x)
 
 
 
         
  • 1
  • 2
  • 3
  • 4

手動控制樣式

可調整選項

摘自prettytable文檔

  • border - 布爾類型參數(必須是True或False)。控制表格邊框是否顯示。
  • header - 布爾類型參數(必須是True或False)。控制表格第一行是否作為表頭顯示。
  • header-style - 控制表頭信息的大小寫。允許的參數值:“cap”(每個單詞首字母大寫),“title”(除了介詞助詞首字母大寫),“lower”(全部小寫)或者None(不改變原內容格式)。默認參數為None。
  • hrules - 設置表格內部水平邊線。允許的參數值:FRAME,ALL,NONE。注意這些是在prettytable模塊內部定義的變量,在使用之前導入或用類似prettytable.FRAME的方法調用。
  • vrules - 設置表格內部豎直邊線。允許的參數值:FRAME,ALL,NONE。
  • align - 水平對齊方式(None,“l”(左對齊),“c”(居中),“r”右對齊)
  • valign - 垂直對齊方式(None,“t”(頂部對齊),“m”(居中),“b”底部對齊)
  • int_format - 控制整型數據的格式。
  • float_format - 控制浮點型數據的格式。
  • padding_width - 列數據左右的空格數量。(當左右padding未設置時生效)
  • left_padding_width - 列數據左側的空格數量。
  • right_padding_width - 列數據右側的空格數量。
  • vertical_char - 繪制豎直邊線的字符,默認為“|”
  • horizontal_char - 繪制水平邊線的字符,默認為“-”
  • junction_char - 繪制水平豎直交匯點的字符,默認為“+”

  • border - A boolean option (must be True or False). Controls whether or not a border is drawn around the table.
  • header - A boolean option (must be True or False). Controls whether or not the first row of the table is a header showing the names of all the fields.
  • header_style - Controls capitalisation of field names in the header. Allowed values: “cap” (capitalise first letter of each word), “title” (title case), “upper” (all upper-case), “lower” (all lower-case) or None (don’t change from original field name setting). Default is None.
  • hrules - Controls printing of horizontal rules after rows. Allowed values: FRAME, ALL, NONE - note that these are variables defined inside the prettytable module so make sure you import them or use prettytable.FRAME etc.
  • vrules - Controls printing of vertical rules between columns. Allowed values: FRAME, ALL, NONE
  • align - Horizontal alignment (None, “l” (left), “c” (centre), “r” (right))
  • valign - Vertical alignment (None, “t” (top), “m” (middle) or “b” (bottom))
  • int_format - Controls formatting of integer data. This should be a string which can be placed between “%” and “d” in something like print “%d” % 42.
  • float_format - Controls formatting of floating point data. This should be a string which can be placed between “%” and “f” in something like print “%f” % 4.2.
  • padding_width - Number of spaces on either side of column data (only used if left and right paddings are None).
  • left_padding_width - Number of spaces on left hand side of column data.
  • right_padding_width - Number of spaces on right hand side of column data.
  • vertical_char - Single character string used to draw vertical lines. Default is |.
  • horizontal_char - Single character string used to draw horizontal lines. Default is -.
  • junction_char - Single character string used to draw line junctions. Default is +.
用法
x = PrettyTable() 
x.border = False 
x.header = False 
x.padding_width = 5
 
 
 
         
  • 1
  • 2
  • 3
  • 4
x = PrettyTable(border=False, header=False, padding_width=5)
 
 
 
         
  • 1

以上兩種設置方式等效

調整對齊方式的幾種方法
print(x.get_string(align="l"))
 
 
 
         
  • 1
x.align["City name"] = "l" 
x.align["Population"] = "c" 
x.align["Area"] = "r"
 
 
 
         
  • 1
  • 2
  • 3
x.align = "l'
 
 
 
         
  • 1

參考資料


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM