python对excel操作
学习一下:原文链接:http://www.cnblogs.com/lhj588/archive/2012/01/06/2314181.html
一、安装xlrd模块
到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了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操作的更多相关文章
- Python对Excel操作详解
Python对Excel操作详解 文档摘要: 本文档主要介绍如何通过python对office excel进行读写操作,使用了xlrd.xlwt和xlutils模块.另外还演示了如何通过Tcl ...
- python 对excel操作用法详解
在python中,对excel表格读,写,追加数据,用以下三个模块: 1.wlrd 读取excel表中的数据 2.xlwt 创建一个全新的excel文件,然后对这个文件进行写入内容以及保存. 3.xl ...
- python 对excel操作
在python中,对excel表格读,写,追加数据,用以下三个模块:1.wlrd 读取excel表中的数据2.xlwt 创建一个全新的excel文件,然后对这个文件进行写入内容以及保存.3.xluti ...
- Python 针对Excel操作
1.python 读取Excel # -*- coding: utf-8 -*- import xlrd import os,sys reload(sys) sys.setdefaultencodin ...
- 3分钟学会Python 针对Excel操作
1.python 读取Excel # -*- coding: utf-8 -*- import xlrd import os,sys reload(sys) sys.setdefaultencodin ...
- Python的Excel操作及数据可视化
Excel表操作 python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写excel的库. 安装xlrd pip install xlrd 简单的表格读取 ...
- 【Python】Excel操作-1
#练习:创建Excel 如果要创建的Excel已经存在并打开,会报错 from openpyxl import Workbook wb=Workbook() #创建文件对象 ws=wb.active ...
- 爬虫入门【9】Python链接Excel操作详解-openpyxl库
Openpyx是一个用于读写Excel2010各种xlsx/xlsm/xltx/xltm文件的python库. 现在大多数用的都是office2010了,如果之前之前版本的可以使用xlrd读,xlwt ...
- 【Python】Excel操作-2 (07版本以下Excel操作,其实不怎么用了,麻蛋,预习了2天课间才发现,还说怎么跟老师讲的不一样)
#保存修改Excel import xlrd from xlutils.copy import copy #打开Excel文档并将内容读取到内存 readbook=xlrd.open_workbook ...
随机推荐
- Makefile详解
原文链接:https://blog.csdn.net/qq_38646470/article/details/79917494 专栏链接:https://blog.csdn.net/column/de ...
- C#_Switch语句的内部实现
Switch的C#内部实现 https://www.cnblogs.com/Interkey/p/3730432.html 在IL汇编语言中的Switch指令 -- 按照标号来进行跳转(和goto语句 ...
- CHAPTER 25 The Greatest Show on Earth 第25章 地球上最壮观的演出
CHAPTER 25 The Greatest Show on Earth 第25章 地球上最壮观的演出 Go for a walk in the countryside and you will f ...
- Azure-如何排查应用程序网关返回 HTTP Code 502 或客户端得到应用程序网关响应慢的问题(二)
问题描述 经过如何排查应用程序网关返回 HTTP Code 502 或客户端得到应用程序网关响应慢的问题(一)中的排查步骤,可以判断出是由于 Web 服务器自身问题导致的响应异常. 那么可以在 IIS ...
- TensorFlow中的卷积函数
前言 最近尝试看TensorFlow中Slim模块的代码,看的比较郁闷,所以试着写点小的代码,动手验证相关的操作,以增加直观性. 卷积函数 slim模块的conv2d函数,是二维卷积接口,顺着源代码可 ...
- LeetCode 174. Dungeon Game (C++)
题目: The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dung ...
- mysql更新表数据时报错 You can't specify target table 'RES_CATALOG_CLASSIFY' for update in FROM clause
You can't specify target table for update in FROM clause含义:不能在同一表中查询的数据作为同一表的更新数据. 将sql语句 UPDATE RES ...
- 20172319 《Java程序设计教程》 第9周学习总结
20172319 2018.05.06-05.14 <Java程序设计教程>第9周学习总结 目录 教材学习内容总结 教材学习中的问题和解决过程 代码调试中的问题和解决过程 代码托管 上周考 ...
- Linux下查看cpu使用率
top命令 是Linux下常用的性能 分析工具 ,能够实时显示系统 中各个进程的资源占用状况,类似于Windows的任务管理 器.下面详细介绍它的使用方法. top - 02:53:32 up 16 ...
- tcp/ip客户端与服务器
单击“发送数据”把数据发送到指定IP地址的指定端口号 using System; using System.Collections.Generic; using System.ComponentMod ...