安裝:pip install PrettyTable
普通表格
from prettytable import PrettyTable
table = PrettyTable(['編號(hào)','云編號(hào)','名稱','IP地址'])
table.add_row(['1','server01','服務(wù)器01','172.16.0.1'])
table.add_row(['2','server02','服務(wù)器02','172.16.0.2'])
table.add_row(['3','server03','服務(wù)器03','172.16.0.3'])
table.add_row(['4','server04','服務(wù)器04服務(wù)器04','172.16.0.4'])
table.add_row(['5','server05','服務(wù)器05','172.16.0.5'])
table.add_row(['6','server06','服務(wù)器06','172.16.0.6'])
table.add_row(['7','server07','服務(wù)器07','172.16.0.7'])
table.add_row(['8','server08','服務(wù)器08','172.16.0.8'])
table.add_row(['9','server09','服務(wù)器09','172.16.0.9'])
print(table)
定義函數(shù)打印dataframe
from prettytable import PrettyTable
def print_pretty(df0):
df=df0.reset_index(inplace=False)
columns=list(df.columns)
table = PrettyTable(columns)
rowNum = df.shape[0]
for i in range(0,rowNum-1):
table.add_row(list(df.iloc[i]))
print(table.get_string(align="r"))
SQL數(shù)據(jù)庫(kù)表轉(zhuǎn)prettytable
import sqlite3 as lite
from prettytable import from_db_cursor
con = lite.connect('data.db')
with con:
cur = con.cursor()
cur.execute('SELECT * FROM Cities')
x = from_db_cursor(cur)
print(x)
CSV轉(zhuǎn)prettytable
import sys
from prettytable import PrettyTable
from prettytable import from_csv
reload(sys)
sys.setdefaultencoding('utf8')
table = PrettyTable()
fp = open("res.csv", "r")
table = from_csv(fp)
print(table)
fp.close()
詳細(xì)參閱:
https:///python/python-tutorial/python-prettytable.html
https:///blog/python/prettytable.html
https://blog.csdn.net/xc_zhou/article/details/81458740
|