您好,登錄后才能下訂單哦!
這篇文章主要介紹“python怎么利用PrettyTable美化表格”,在日常操作中,相信很多人在python怎么利用PrettyTable美化表格問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”python怎么利用PrettyTable美化表格”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
pip install PrettyTable
import prettytable as pt # 按行添加數據 tb = pt.PrettyTable() tb.field_names = ['name', 'age', 'height', 'weight'] tb.add_row(['autofelix', 25, 174, 65]) tb.add_row(['大神', 23, 164, 55]) tb.add_row(['飛兔小哥', 27, 184, 69.5]) print(tb) # +-----------+-----+--------+--------+ # | name | age | height | weight | # +-----------+-----+--------+--------+ # | autofelix | 25 | 174 | 65 | # | 大神 | 23 | 164 | 55 | # | 飛兔小哥 | 27 | 184 | 69.5 | # +-----------+-----+--------+--------+
import prettytable as pt # 按行添加數據 tb = pt.PrettyTable() tb.field_names = ['name', 'age', 'height', 'weight'] tb.add_row(['autofelix', 25, 174, 65]) tb.add_row(['大神', 23, 164, 55]) tb.add_row(['飛兔小哥', 27, 184, 69.5]) # 按列添加數據 tb.add_column('sex',['男', '女', '男']) print(tb) # +-----------+-----+--------+--------+-----+ # | name | age | height | weight | sex | # +-----------+-----+--------+--------+-----+ # | autofelix | 25 | 174 | 65 | 男 | # | 大神 | 23 | 164 | 55 | 女 | # | 飛兔小哥 | 27 | 184 | 69.5 | 男 | # +-----------+-----+--------+--------+-----+
MSWORD_FRIENDLY:MSWORD_FRIENDLY輸出風格
PLAIN_COLUMNS:PLAIN_COLUMNS輸出風格
RANDOM:每次隨機輸出風格
DEFAULT:默認輸出風格
import prettytable as pt # 按行添加數據 tb = pt.PrettyTable() tb.field_names = ['name', 'age', 'height', 'weight'] tb.add_row(['autofelix', 25, 174, 65]) tb.add_row(['大神', 23, 164, 55]) tb.add_row(['飛兔小哥', 27, 184, 69.5]) # 風格 tb.set_style(pt.MSWORD_FRIENDLY) print(tb) # | name | age | height | weight | # | autofelix | 25 | 174 | 65 | # | 大神 | 23 | 164 | 55 | # | 飛兔小哥 | 27 | 184 | 69.5 |
import prettytable as pt # 按行添加數據 tb = pt.PrettyTable() tb.field_names = ['name', 'age', 'height', 'weight'] tb.add_row(['autofelix', 25, 174, 65]) tb.add_row(['大神', 23, 164, 55]) tb.add_row(['飛兔小哥', 27, 184, 69.5]) # 不打印,獲取表格字符串 s1 = tb.get_string() print(s1) # +-----------+-----+--------+--------+ # | name | age | height | weight | # +-----------+-----+--------+--------+ # | autofelix | 25 | 174 | 65 | # | 大神 | 23 | 164 | 55 | # | 飛兔小哥 | 27 | 184 | 69.5 | # +-----------+-----+--------+--------+ # 或者可以只獲取指定列或行 s2 = tb.get_string(fields=['name', 'age'], start=1, end=4) print(s2) # +----------+-----+ # | name | age | # +----------+-----+ # | 大神 | 23 | # | 飛兔小哥 | 27 | # +----------+-----+
import prettytable as pt # 按行添加數據 tb = pt.PrettyTable() tb.field_names = ['name', 'age', 'height', 'weight'] tb.add_row(['autofelix', 25, 174, 65]) tb.add_row(['大神', 23, 164, 55]) tb.add_row(['飛兔小哥', 27, 184, 69.5]) # 設定左對齊 tb.align = 'l' # 設定數字輸出格式 tb.float_format = '2.2' # 設定邊框連接符為'*" tb.junction_char = '*' # 設定排序方式 tb.sortby = 'age' # 設定左側不填充空白字符 tb.left_padding_width = 0 # 不顯示邊框 # tb.border = 0 # 修改邊框分隔符 tb.horizontal_char = '+' print(tb) # *++++++++++*++++*+++++++*+++++++* # |name |age |height |weight | # *++++++++++*++++*+++++++*+++++++* # |大神 |23 |164 |55 | # |autofelix |25 |174 |65 | # |飛兔小哥 |27 |184 |69.50 | # *++++++++++*++++*+++++++*+++++++*
import prettytable as pt # 按行添加數據 tb = pt.PrettyTable() tb.field_names = ['name', 'age', 'height', 'weight'] tb.add_row(['autofelix', 25, 174, 65]) tb.add_row(['大神', 23, 164, 55]) tb.add_row(['飛兔小哥', 27, 184, 69.5]) # 輸出HTML代碼 s = tb.get_html_string() print(s) # <table> # <thead> # <tr> # <th>name</th> # <th>age</th> # <th>height</th> # <th>weight</th> # </tr> # </thead> # <tbody> # <tr> # <td>autofelix</td> # <td>25</td> # <td>174</td> # <td>65</td> # </tr> # <tr> # <td>大神</td> # <td>23</td> # <td>164</td> # <td>55</td> # </tr> # <tr> # <td>飛兔小哥</td> # <td>27</td> # <td>184</td> # <td>69.5</td> # </tr> # </tbody> # </table>
import prettytable as pt # 按行添加數據 tb = pt.PrettyTable() tb.field_names = ['name', 'age', 'height', 'weight'] tb.add_row(['autofelix', 25, 174, 65]) tb.add_row(['大神', 23, 164, 55]) tb.add_row(['飛兔小哥', 27, 184, 69.5]) tb.horizontal_char = '.' tb2 = tb.copy() tb.align = 'l' tb2.align = 'r' print(tb) print(tb2) # +...........+.....+........+........+ # | name | age | height | weight | # +...........+.....+........+........+ # | autofelix | 25 | 174 | 65 | # | 大神 | 23 | 164 | 55 | # | 飛兔小哥 | 27 | 184 | 69.5 | # +...........+.....+........+........+ # +...........+.....+........+........+ # | name | age | height | weight | # +...........+.....+........+........+ # | autofelix | 25 | 174 | 65 | # | 大神 | 23 | 164 | 55 | # | 飛兔小哥 | 27 | 184 | 69.5 | # +...........+.....+........+........+
到此,關于“python怎么利用PrettyTable美化表格”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。