import prettytable as pt

#直接创建表
tb = pt.PrettyTable()
#表头字段
tb.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
#按行添加数据
tb.add_row(["Adelaide",1295, 1158259, 600.5])
tb.add_row(["Brisbane",5905, 1857594, 1146.4])
tb.add_row(["Darwin", 112, 120900, 1714.7])
tb.add_row(["Hobart", 1357, 205556,619.5])

print(tb)

# 按列添加数据
tb.add_column("index",[1,2,3,4])

print(tb)

# 获取指定列的数据,start是起始行,起始行从0开始,end是截止行
s = tb.get_string(fields=["City name", "Population"],start=0,end=1)

print(s)

 

posted on 2019-04-12 14:41  bainianminguo  阅读(171)  评论(0)    收藏  举报