python学习_python怎么输出表格

2021-05-21 0 1,112 百度已收录

可使用PrettyTable 是python中的一个第三方库,可用来天生美妙的ASCII格局的表格:

python学习_python怎么输出表格

PrettyTable装置(推选进修:Python视频教程)

pip install prettytable

示例

import prettytable as pt
# tb = pt.PrettyTable( ["City name", "Area", "Population", "Annual Rainfall"])
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)

输入:

+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
|  Adelaide | 1295 |  1158259   |      600.5      |
|  Brisbane | 5905 |  1857594   |      1146.4     |
|   Darwin  | 112  |   120900   |      1714.7     |
|   Hobart  | 1357 |   205556   |      619.5      |
+-----------+------+------------+-----------------+

假如不增加表头,那末会以默许的Field+编号表现,比方:

+---------+----------+----------+------------+
| Field 1 | Field 2  | Field 3  |  Field 4   |
+---------+----------+----------+------------+

更多Python相关技能文章,请拜访Python教程栏目停止进修!

以上便是python怎样输入表格的具体内容,更多请存眷酷吧易资源网别的相关文章!

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

酷吧易资源网 python教程 python学习_python怎么输出表格 https://www.kubayi.com/4915.html

常见问题

相关文章

评论
暂无评论