python中prettytable模块_Python prettytable模块

本文介绍如何使用Python的prettytable模块创建整洁的表格,包括模块安装、基本用法和示例,展示了如何定义列宽、对齐方式等,让你的数据以美观的形式呈现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python  prettytable模块

Python通过prettytable模块将输出内容

如表格方式整齐输出:

首先下载prettytable模块,

下载连接:https://pypi.python.org/pypi/PrettyTable

下载后解压:

wget https://pypi.python.org/packages/source/P/PrettyTable/prettytable-0.7.2.tar.gz#md5=a6b80afeef286ce66733d54a0296b13b

tar zxf prettytable-0.7.2.tar.gz

1:cp prettytable.py  /usr/lib/python2.6/site-packages/

2:chmod a+rx  /usr/lib/python2.6/site-packages/prettytable.py

Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56)

[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> from prettytable import PrettyTable

>>>

>>> x = PrettyTable(["City name", "Area", "Population", "Annual Rainfall"])

>>> x.align["City name"] = "l"# Left align city names

>>> x.padding_width = 1# One space between column edges and contents (default)

>>> x.add_row(["Brisbane",5905, 1857594, 1146.4])

>>> x.add_row(["Hobart", 1357, 205556, 619.5])

>>> print x

+-----------+------+------------+-----------------+

| City name | Area | Population | Annual Rainfall |

+-----------+------+------------+-----------------+

| Brisbane  | 5905 |  1857594   |      1146.4     |

| Hobart    | 1357 |   205556   |      619.5      |

+-----------+------+------------+-----------------+

>>>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值