from prettytable import PrettyTabl
创建表设置字段
# 创建一个表
table = PrettyTable()
# 设置字段
table.field_names = ['id','name','age']
# 创建一个表有,并设置字段
table = PrettyTable(['id','name','age'])
添加数据
# 添加一行数据
table.add_row(['001','可恶的小猪','23'])
通过已知文件创建
# 通过csv创建
from prettytable import from_csv
csv_file = open('csv_file.csv','r')
table = from_csv(csv_file)
table.add_row(['005','钢铁小蘑菇','33'])
print(table)
# 通过html创建
from prettytable import from_html
table = from_html(html_string)
# 通过数据库中的表创建
from prettytable import from_db_cursor
db_cur.execute("SELECT * FROM mytable")
pt = from_db_cursor(db_cur)
注意: 这里的db_cur为对数据进行操作的python变量.需要你导入数据库进行操作
输出
# 输出为无表格
print(pt.get_string())
# 输出为html
print(pt.get_html_string())
# 带样式的输出
table.set_style(MSWORD_FRIENDLY)
print(table)