python prettytable_让python的print变得更好看——prettytable和colorama
【小宅按】python在輸出信息的時候,通常會使用print,有沒有方法可以美化一下輸出的格式呢?
這里介紹兩個包:prettytable
colorama
這兩個包都不是Python的標準庫,因此在使用之前,需要安裝它們。
pip install prettytable colorama
prettytable
prettytable可以發方便地對print的內容進行格式化的表格輸出。
例如:
from prettytable import PrettyTable
table = PrettyTable(["name", "score"])
table.add_row(["Bob", 67])
table.add_row(["grizzly", 45])
table.add_row(["Tom of Caerbannog", 78])
table.add_row(["cat", 34])
table.add_row(["Tony", 39])
table.add_row(["dolphin", 45])
table.add_row(["albatross", 24])
table.sort_key("name")
table.reversesort = True
print(table)輸出的內容
colorama
Python的Colorama模塊,可以跨多終端,顯示字體不同的顏色和背景,常用的參數有以下幾個:Fore: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
Back: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
Style: DIM, NORMAL, BRIGHT, RESET_ALL
下面我們來看一下這些參數是怎么使用的吧!
from colorama import Fore, Back, Style
print(Fore.RED + "some red text")
print(Back.GREEN + "and with a green background")
print(Style.DIM + "and in dim text")
print(Style.RESET_ALL)
print("back to normal now!!")輸出結果
colorama詳細的使用技巧,可以查看可以結合python異常處理機制,將錯誤信息用不同顏色打印
大量格式化信息打印的時候,可以結合prettytable輸出很棒的表格
你總是能在眾多的包中找到自己想要的,而且使用起來也非常簡單,也許這就是Python這么流行的原因之一吧!
更多精彩內容,請滑至頂部點擊右上角關注小宅哦~
總結
以上是生活随笔為你收集整理的python prettytable_让python的print变得更好看——prettytable和colorama的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《概率论与数理统计》(浙大第四版)第四章
- 下一篇: MYSQL的函数有哪些?(4.3时间与日