本文主要介绍xlrd模块读取Excel文档的基本用法,并以一个GDP数据的文档为例来进行操作。

1. 准备工作:

1. 安装xlrd:pip install xlrd

2. 准备数据集:从网上找到的1952~2012年中国国内GDP的数据,数据结构如下:

2. 目标:将这份数据转换成json格式的数据

3. 上代码

#!/usr/bin/python
# coding:utf-8
# 用xlrd读取Excel文件基本用法
import sys
import xlrd
import json # 设置编码格式
reload(sys)
sys.setdefaultencoding('utf-8') # 1. 从Excel文件中读取出Book对象
data = xlrd.open_workbook('./gdp_data.xls')
# print type(data)
# 输出:<class 'xlrd.book.Book'> # 2. 获取sheet页对象
# 2.1 通过sheet索引获取
sheet1 = data.sheet_by_index(0)
# print sheet1
# 输出:<xlrd.sheet.Sheet object at 0x7efc10319ed0> # 2.2 通过sheet名称获取
sheet2 = data.sheet_by_name(u'Sheet1')
# print sheet2
# 输出:<xlrd.sheet.Sheet object at 0x7efbfb72db10> # 3. 获取sheet页的行数和列数
nrows = sheet1.nrows
ncols = sheet1.ncols
# print nrows,ncols
# 输出:62 5
# 说明表格有62行、5列 # 4. 获取第0行的值(是一个列表)
row_data = sheet1.row_values(0)
# print row_data
# 输出:[u'year', u'GDP', u'first industry', u'second industry', u'third industry'] # 5. 获取第0列的值(是一个列表)
col_data = sheet1.col_values(0)
# print col_data
# 输出:[u'year', 1952.0, 1953.0, 1954.0, 1955.0,...] # 6. 使用行列索引(从0开始)获取单元格的数据
cell_A1 = sheet1.cell(0,0)
# print cell_A1
# print type(cell_A1)
# print cell_A1.value
# 输出:
'''
text:u'year'
<class 'xlrd.sheet.Cell'>
year
''' # 7. 应用:将Excel文件中的数据转换成json数组
# 索引(即表头)
idx = sheet1.row_values(0)
# 最终的数据列表
data = []
# 从第1行开始遍历循环所有行,获取每行的数据
for i in range(1,nrows):
row_data = sheet1.row_values(i)
# 组建每一行数据的字典
row_data_dict = {}
# 遍历行数据的每一项,赋值进行数据字典
for j in range(len(row_data)):
item = row_data[j]
row_data_dict[idx[j]] = item
# 将年份字段转成整形
row_data_dict['year'] = int(row_data_dict['year'])
# 将行数据字典加入到data列表中
data.append(row_data_dict) print json.dumps(data,indent = 4)
# 输出:
'''
[
{
"GDP": 679.0,
"second industry": 141.8,
"first industry": 342.9,
"third industry": 194.3,
"year": 1952
},
{
"GDP": 824.0,
"second industry": 192.5,
"first industry": 378.0,
"third industry": 253.5,
"year": 1953
},
{
"GDP": 859.0,
"second industry": 211.7,
"first industry": 392.0,
"third industry": 255.3,
"year": 1954
},
...
]
'''

随机推荐

  1. IPBX和话机对接

    某厂家的话机和IPBX进行对接问题: 1.      该话机作为主叫方,呼叫能够正常建立 2.      该话机作为被叫方.呼叫无法建立,IPBX发送INVITE消息给该话机,该话机回复400 具体消 ...

  2. OpenCV学习笔记七:opencv_nonfree模块

    一,简介: 顾名思义,这个模块不是free的.主要包含: 1,SIFT implementation. The class implements SIFT algorithm by D. Lowe. ...

  3. urllib基本使用-Handler和自定义的opener()

    """ 基本的urlopen()方法不支持代理.cookie等其他的HTTP/HTTPS高级功能.所以要支持这些功能: 使用相关的 Handler处理器 来创建特定功能的 ...

  4. PHP POST, GET 参数过滤,预防sql注入函数

    1. 实际过滤函数 可适当修改其中的正则表示式 1 static public function filterWords(&$str) 2 { 3 $farr = array( 4 " ...

  5. yii2.0 干货

    Yii2 干货集,欢迎提交 Pull Requests.(提交过来的开源项目最好是你用过的,并且觉得好用的) Docs 文档 Yii Framework 2.0 类参考手册 Yii Framework ...

  6. linux之shell之if、while、for语句介绍

    一.基本判断条件 1)逻辑运算符 -a    expr1 -a expr2    逻辑与 -o    expr1 -o expr2    逻辑或 !     !expr1                ...

  7. python redis操作

    import redis r = redis.Redis( host='1xx.x24.3xx.x0', #ip, password='xnxnxn&*',#密码 port=6379, #端口 ...

  8. HTML学习笔记——常用元素及其属性(一)

    1.img 标签 -- 代表HTML图像 img标签是单独出现的,<img /> 语法: <img src="URI" alt="alttext&quo ...

  9. 《从零开始学Swift》学习笔记(Day48)——类型检查与转换

    原创文章,欢迎转载.转载请注明:关东升的博客 继承会发生在子类和父类之间,是一系列类的继承关系. 例如:Person是类层次结构中的根类,Student是Person的直接子类,Worker是Pers ...

  10. echarts, 小知识点随意记录,

    注意点: 写echarts代码时,注意格式,每个项的子项用‘,‘分隔,最后一项不需要.如符号不也会造成图形显示不出来.写时注意参照配置项. 如下orient的属性需要单引号,每个项需要逗号等. leg ...