没有合适的资源?快使用搜索试试~ 我知道了~
prettyTable 是一款很简洁但是功能强大的第三方模块,主要是将输入的数据转化为格式化的形式来输出,即:以表格的形式的打印输出出来,能够起到美观的效果,今天简单地试用了一下, 一、下载与安装 进入pypi.python.org查找并下载PrettyTable将其放在Python文件夹下的Scripts文件夹下 进入命令提示符工具,转到Scripts文件夹下,通过命令pip install prettytable-0.7.2.tar.bz2安装该模块 二、简单的使用 导入该模块 from prettytable import PrettyTable 创建表头 table=P
资源推荐
资源详情
资源评论
python PrettyTable模块的安装与简单应用模块的安装与简单应用
prettyTable 是一款很简洁但是功能强大的第三方模块,主要是将输入的数据转化为格式化的形式来输出,即:以表格的形式
的打印输出出来,能够起到美观的效果,今天简单地试用了一下,
一、下载与安装一、下载与安装
进入pypi.python.org查找并下载PrettyTable将其放在Python文件夹下的Scripts文件夹下
进入命令提示符工具,转到Scripts文件夹下,通过命令pip install prettytable-0.7.2.tar.bz2安装该模块
二、简单的使用二、简单的使用
导入该模块
from prettytable import PrettyTable
创建表头
table=PrettyTable(["姓名","学号","性别"])
插入数据
table.add_row(["小明","01","男"])
table.add_row(["小红","02","女"])
table.add_row(["小黄","03","男"])
显示该表
print(table)
三、下面是具体的实践:三、下面是具体的实践:
#!usr/bin/env python
#encoding:utf-8
'''
__Author__:沂水寒城
功能: PrettyTable 模块使用
'''
import prettytable
from prettytable import from_csv
from prettytable import PrettyTable
def testFunc1():
'''
'''
table=PrettyTable()
table.field_names = ["City name", "Area", "Population", "Annual Rainfall"] table.add_row(["Adelaide",1295, 1158259, 600.5])
table.add_row(["Brisbane",5905, 1857594, 1146.4])
table.add_row(["Darwin", 112, 120900, 1714.7])
table.add_row(["Hobart", 1357, 205556, 619.5])
table.add_row(["Sydney", 2058, 4336374, 1214.8])
table.add_row(["Melbourne", 1566, 3806092, 646.9])
table.add_row(["Perth", 5386, 1554769, 869.4])
print '=================================table===================================='
print table
table.add_column("City name",["Adelaide","Brisbane","Darwin","Hobart","Sydney","Melbourne","Perth"])
table.add_column("Area",[1295, 5905, 112, 1357, 2058, 1566, 5386])
table.add_column("Population",[1158259, 1857594, 120900, 205556, 4336374, 3806092,1554769])
table.add_column("Annual Rainfall",[600.5, 1146.4, 1714.7, 619.5, 1214.8, 646.9,869.4])
print '=================================table===================================='
print table
def testFunc2(data='mycsv.csv'):
'''
从 csv 文件中加载数据
'''
mycsv=open(data)
table=from_csv(mycsv)
mycsv.close()
print
'===========================================table=============================================='
print table
print
'=================================table:SepalLength_Species===================================='
print table.get_string(fields=['SepalLength','Species'])
print '=======================================table:60=>80
rows======================================'
print table.get_string(start=60,end=80)
if __name__=='__main__':
testFunc1()
testFunc2(data='iris.csv')
结果如下:
=================================table====================================
+-----------+------+------------+-----------------+
| 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 |
| Sydney | 2058 | 4336374 | 1214.8 |
| Melbourne | 1566 | 3806092 | 646.9 |
| Perth | 5386 | 1554769 | 869.4 |
+-----------+------+------------+-----------------+
=================================table====================================
+-----------+------+------------+-----------------+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall | City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+-----------+------+------------+-----------------+
| Adelaide | 1295 | 1158259 | 600.5 | Adelaide | 1295 | 1158259 | 600.5 |
| Brisbane | 5905 | 1857594 | 1146.4 | Brisbane | 5905 | 1857594 | 1146.4 |
| Darwin | 112 | 120900 | 1714.7 | Darwin | 112 | 120900 | 1714.7 |
| Hobart | 1357 | 205556 | 619.5 | Hobart | 1357 | 205556 | 619.5 |
| Sydney | 2058 | 4336374 | 1214.8 | Sydney | 2058 | 4336374 | 1214.8 |
| Melbourne | 1566 | 3806092 | 646.9 | Melbourne | 1566 | 3806092 | 646.9 |
| Perth | 5386 | 1554769 | 869.4 | Perth | 5386 | 1554769 | 869.4 |
+-----------+------+------------+-----------------+-----------+------+------------+-----------------+
===========================================table==============================================
+-----+-------------+------------+-------------+------------+------------+
| id | SepalLength | SepalWidth | PetalLength | PetalWidth | Species |
+-----+-------------+------------+-------------+------------+------------+
| 1 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 2 | 4.9 | 3 | 1.4 | 0.2 | setosa |
| 3 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
| 4 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
| 5 | 5 | 3.6 | 1.4 | 0.2 | setosa |
| 6 | 5.4 | 3.9 | 1.7 | 0.4 | setosa |
| 7 | 4.6 | 3.4 | 1.4 | 0.3 | setosa |
| 8 | 5 | 3.4 | 1.5 | 0.2 | setosa |
| 9 | 4.4 | 2.9 | 1.4 | 0.2 | setosa |
| 10 | 4.9 | 3.1 | 1.5 | 0.1 | setosa |
| 11 | 5.4 | 3.7 | 1.5 | 0.2 | setosa |
| 12 | 4.8 | 3.4 | 1.6 | 0.2 | setosa |
| 13 | 4.8 | 3 | 1.4 | 0.1 | setosa |
| 14 | 4.3 | 3 | 1.1 | 0.1 | setosa |
| 15 | 5.8 | 4 | 1.2 | 0.2 | setosa |
| 16 | 5.7 | 4.4 | 1.5 | 0.4 | setosa |
| 17 | 5.4 | 3.9 | 1.3 | 0.4 | setosa |
| 18 | 5.1 | 3.5 | 1.4 | 0.3 | setosa |
| 19 | 5.7 | 3.8 | 1.7 | 0.3 | setosa |
| 20 | 5.1 | 3.8 | 1.5 | 0.3 | setosa |
| 21 | 5.4 | 3.4 | 1.7 | 0.2 | setosa |
剩余7页未读,继续阅读
资源评论
weixin_38507121
- 粉丝: 10
- 资源: 928
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功