Python读取excel表格操作之xlrd
来源:互联网 发布:java面向对象上机程序 编辑:程序博客网 时间:2024/04/30 00:41
因工作需要要查询excel表格里面的数据。虽然说手动查查也比较简单,但是工作量太大比较浪费时间,既然学习了Python何不用程序帮忙呢?
因为之前没有用过excel模块的。所以得重新学习。
一、安装xlrd模块
到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了python 环境。
注意要选择跟自己使用的python版本一样,免得调用该模块时会报错。
二、使用介绍
1、导入模块
import xlrd
2、打开Excel文件读取数据
data = xlrd.open_workbook('excelFile.xls')
3、使用技巧
获取一个工作表
三、Demo代码
Demo代码其实很简单,就是读取Excel数据。
# -*- coding: utf-8 -*-
import xdrlib ,sys
import xlrd
def open_excel(file= 'file.xls'):
try:
data = xlrd.open_workbook(file)
return data
except Exception,e:
print str(e)
#根据索引获取Excel表格中的数据 参数:file:Excel文件路径 colnameindex:表头列名所在行的所以 ,by_index:表的索引
def excel_table_byindex(file= 'file.xls',colnameindex=0,by_index=0):
data = open_excel(file)
table = data.sheets()[by_index]
nrows = table.nrows #行数
ncols = table.ncols #列数
colnames = table.row_values(colnameindex) #某一行数据
list =[]
for rownum in range(1,nrows):
row = table.row_values(rownum)
if row:
app = {}
for i in range(len(colnames)):
app[colnames[i]] = row[i]
list.append(app)
return list
#根据名称获取Excel表格中的数据 参数:file:Excel文件路径 colnameindex:表头列名所在行的所以 ,by_name:Sheet1名称
def excel_table_byname(file= 'file.xls',colnameindex=0,by_name=u'Sheet1'):
data = open_excel(file)
table = data.sheet_by_name(by_name)
nrows = table.nrows #行数
colnames = table.row_values(colnameindex) #某一行数据
list =[]
for rownum in range(1,nrows):
row = table.row_values(rownum)
if row:
app = {}
for i in range(len(colnames)):
app[colnames[i]] = row[i]
list.append(app)
return list
def main():
tables = excel_table_byindex()
for row in tables:
print row
tables = excel_table_byname()
for row in tables:
print row
if __name__=="__main__":
main()
- Python读取excel表格操作之xlrd
- 用xlrd读取excel表格(python)
- python操作excel之读取数据——xlrd
- python操作excel之xlrd
- python操作excel之xlrd
- Python操作Excel之xlrd
- python操作excel表格(xlrd/xlwt)
- python操作excel表格(xlrd/xlwt)
- python读取excel表格——xlrd的使用
- python的xlrd模块(读取excel表格)
- python操作Excel读写之使用xlrd
- Python操作Excel--xlrd
- [python]使用xlrd对Excel表格进行读写操作
- python中使用xlrd、xlwt操作excel表格详解
- python中使用xlrd、xlwt操作excel表格详解
- python中使用xlrd、xlwt操作excel表格详解
- python中使用xlrd、xlwt操作excel表格详解
- python中使用xlrd、xlwt操作excel表格详解
- Android: Hide Navigation bar 隐藏导航条
- 小世界网络
- Linux文件系统只读Read-only file system的解决方法
- git pull
- 利用@media screen实现网页布局的自适应
- Python读取excel表格操作之xlrd
- 浅谈android 屏幕适配中 dp和sp的区别
- Webservise的使用(C#创建,Android调用)
- 统计查询
- 对象引用与对象的区别
- MyEclipse 2015 移动开发教程
- HttpClient 发送网络请求步骤
- 几款值得推荐的android(安卓)开源框架简介
- Oracle 百万行数据查询优化