本文主要介绍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. 基于docker的webrtc开发环境

    在root目录下先放好自定义的constants.py文件再docker run 同时记得修改PUBLIC_IP 可以跨wifi通信 docker pull piasy/apprtc-server d ...

  2. java -- 路径中包含空格怎么处理

    @.使用toURI()方法 String rootPath = this.getClass().getClassLoader().getResource(".").toURI(). ...

  3. 【转】ATL提供的所有转换宏

    在头文件<atlconv.h>中定义了ATL提供的所有转换宏,如: A2CW (LPCSTR) -> (LPCWSTR) A2W     (LPCSTR) -> (LPWSTR ...

  4. HDU2844 Coins 多重背包

    Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  5. ng-model ng-show

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. linux 文件文件夹操作

    文件夹下查询哪些文件含有abc内容: find .|xargs grep -ri "abc"

  7. aspx.cs方法设置webmenthod特性接收ajax请求

    cs代码: public partial class TelerikWebMethod : BasePage//System.Web.UI.Page { protected void Page_Loa ...

  8. linux以下C 利用openssl的AES库加密,解密

    OpenSSL提供了AES加解密算法的API const char *AES_options(void); AES算法状态,是所有支持或者是部分支持. 返回值:"aes(full)" ...

  9. Linux 的字符串截取很有用。有八种方法。

    假设有变量 var=http://www.aaa.com/123.htm 1. # 号截取,删除左边字符,保留右边字符. echo ${var#*//} 其中 var 是变量名,# 号是运算符,*// ...

  10. LeetCode Problem 35:Search Insert Position

    描述:Given a sorted array and a target value, return the index if the target is found. If not, return ...