python中的简易表格prettytable
安装:pip install PrettyTable
普通表格
from prettytable import PrettyTable
table = PrettyTable(['编号','云编号','名称','IP地址'])
table.add_row(['1','server01','服务器01','172.16.0.1'])
table.add_row(['2','server02','服务器02','172.16.0.2'])
table.add_row(['3','server03','服务器03','172.16.0.3'])
table.add_row(['4','server04','服务器04服务器04','172.16.0.4'])
table.add_row(['5','server05','服务器05','172.16.0.5'])
table.add_row(['6','server06','服务器06','172.16.0.6'])
table.add_row(['7','server07','服务器07','172.16.0.7'])
table.add_row(['8','server08','服务器08','172.16.0.8'])
table.add_row(['9','server09','服务器09','172.16.0.9'])
print(table)
定义函数打印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数据库表转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转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()
详细参阅:
https://geek-docs.com/python/python-tutorial/python-prettytable.html
https://linuxops.org/blog/python/prettytable.html
https://blog.csdn.net/xc_zhou/article/details/81458740