一直想将自己接触到的东西梳理一遍,可就是迈不出第一步,希望从这篇总结开始不要再做行动的矮人了。

最近测试过程中需要用到python读取excel用例数据,于是去了解和学习了下xlrd库,这里只记录使用过程中读取excel数据相关操作。

一、安装xlrd库

  可以下载xlrd库包到本地安装,也可以通过pip命令安装,这里我选择pip命令:

  pip install xlrd

二、使用xlrd读取excel数据

  具体详细的操作可以参考xlrd库操作说明文档,以下是两种读取excel数据的方法:

  1、根据Excel中sheet名称读取数据:    

 def readExcelDataByName(fileName, sheetName):
table = None
errorMsg = None
try:
data = xlrd.open_workbook(fileName)
table = data.sheet_by_name(sheetName)
except Exception, msg:
errorMsg = msg
return table, errorMsg

  2、根据Excel中sheet的序号获取:

 def readExcelDataByIndex(fileName, sheetIndex):
table = None
errorMsg = ""
try:
data = xlrd.open_workbook(fileName)
table = data.sheet_by_index(sheetIndex)
except Exception, msg:
errorMsg = msg
return table, errorMsg

  3、根据列名获取相应序号,由于有时读取excel中列数据时,需要通过列头名称获取相应的列中的值,所以写了下面这个返回列名所在表格中的index。然后就可以直接通过table.cell_value(i, getColumnIndex(table,'列名'))获取列的值。

 def getColumnIndex(table, columnName):
columnIndex = None
for i in range(table.ncols):
if(table.cell_value(0, i) == columnName):
columnIndex = i
break
return columnIndex

  下面加入需要读取如下excel表格中的数据,在读取数据时直接根据列名去获取相应的值。

根据列名读取相应的值,代码如下:

 #!/usr/bin/python
# coding=utf-8
__author__ = 'Paul'
import xlrd
import chardet
import traceback
def getColumnIndex(table, columnName):
columnIndex = None
#print table
for i in range(table.ncols):
#print columnName
#print table.cell_value(0, i)
if(table.cell_value(0, i) == columnName):
columnIndex = i
break
return columnIndex
def readExcelDataByName(fileName, sheetName):
#print fileName
table = None
errorMsg = ""
try:
data = xlrd.open_workbook(fileName)
table = data.sheet_by_name(sheetName)
except Exception, msg:
errorMsg = msg
return table, errorMsg
def readExcelDataByIndex(fileName, sheetIndex):
table = None
errorMsg = ""
try:
data = xlrd.open_workbook(fileName)
table = data.sheet_by_index(sheetIndex)
except Exception, msg:
errorMsg = msg
return table, errorMsg
if __name__ == '__main__':
#example
xlsfile= 'F:/test_AutoTesting/TestCase/RunList.xlsx'
table = readExcelDataByName(xlsfile, 'Sheet1')[0]
#获取第一行的值
testcase_id = table.cell_value(1, getColumnIndex(table,'TestCaseID'))
app_config = table.cell_value(1, getColumnIndex(table,'APPConfig')) print u'测试用例ID为:%s'%(testcase_id)
print u'配置信息为:%s'%(app_config)

得出结果如下:

  4、读取excel中的文本或数值转换成了float的问题

    有时Excel中的值为20,但读取出来的值却变成了20.0,这与我们想要的不大一致,特别是做UI自动化测试过程中需要下拉选择值时就完全选不出想要的选项了。目前我想到的是通过下面的语句来处理:

if isinstance(inputValue,float):    #判断读取到的值是否为float
if inputValue==int(inputValue): #判断读取到的值与转成int后的值是否相等,如果相等则转成int
inputValue = int(inputValue)
inputValue = str(inputValue) #转成str

Python读取Excel数据并根据列名取值的更多相关文章

  1. python 读取excel数据并将测试结果填入Excel

    python 读取excel数据并将测试结果填入Excel 读取一个Excel中的一条数据用例,请求接口,然后返回结果并反填到excel中.过程中会生成请求回来的文本,当然还会生成一个xml文件.具体 ...

  2. Python读取excel 数据

    1.安装xlrd 2.官网 通过官网来查看如何使用python读取Excel,python excel官网: http://www.python-excel.org/ 实例: (1)Excel内容 把 ...

  3. Python读取Excel数据

    今天一同学给我发来一个Excel文件,让我帮他找一些信息,打开一开 8000多条数据.自己手工处理是不可能完成的的啦.作为一名程序员,当然要用程序来处理.处理生活中的问题当然是Python最为方便啦. ...

  4. python读取excel数据并以第一行标题加内容组成字典格式返回

    excel结构如图所示: 代码: import xlrd ''' 通用获取excel数据 @:param path excel文件路径 @:param sheet_name excel文件里面shee ...

  5. python读取excel数据为json格式(兼容xls\xlsx)

    做自动化时需要从excel读取数据: 本文实现将excel文件数据读取为json格式,方便自动化调用 读取xls文件 使用xlrd读取xls文件代码: import xlrd def read_xls ...

  6. python读取excel数据,并可视化展现

    #-*- coding: utf-8 -*- import pandas as pda import matplotlib.pyplot as pyl import matplotlib.font_m ...

  7. python 读取excel数据

    import xlrd book = xlrd.open_workbook(file_path)#打开文件 sheet = book.sheet_by_index(0) #获取第一个工作簿 print ...

  8. Python 读取Excel数据 xlrd

    #导入相关模块 from xlrd import open_workbook #打开excel file = open_workbook("test.xlsx") #获取sheet ...

  9. python 读取excel数据插入到另外一个excel

    #-*-coding:utf-8-*- import xlrd import xlwt def excel_copy(dir_from, dir_to, sheet_name): '''从一个exce ...

随机推荐

  1. 个人信用卡管理 - iOS UI原型

  2. UVA 11475 Extend to Palindrome(后缀数组+ST表)

    [题目链接] http://acm.hust.edu.cn/vjudge/problem/27647 [题目大意] 给出一个字符串,要求在其后面添加最少的字符数,使得其成为一个回文串.并输出这个回文串 ...

  3. [转载]HDFS的'Block'和MapReduce的'Split'之间的关系和区别

    http://www.cnblogs.com/xuxm2007/archive/2011/09/01/2162011.html hadoop的分块有两部分,其中第一部分更为人熟知一点.   第一部分就 ...

  4. 关于typedef之四种用途 和 两个陷进

    typedef用来声明一个别名,typedef后面的语法,是一个声明.本来笔者以为这里不会产生什么误解的,但结果却出乎意料,产生误解的人 不在少数.罪魁祸首又是那些害人的教材.在这些教材中介绍type ...

  5. AsyncSocket的长连接使用

    使用背景:需要跟服务器长期保持连接进行即时通讯:还有在跟智能硬件建立实时链接进行同步智能硬件的状态等,最近我就做项目就碰到需要实时更新智能硬件的状态(比如智能硬件的电量,以及其它工作状态),跟智能硬件 ...

  6. 面试题之String str = new String("abc"); 创建了几个对象

    今天去面试的时候碰到了这个问题:String str = new String("abc"); 创建了几个对象,回来自己研究并查阅资料才发现答错了..网上的争论不少,有的说是两个, ...

  7. ubuntu 16.04环境配置

    ubuntu 16:1.源cp /etc/apt/sources.list /etc/apt/sources.list.bkpvi /etc/apt/sources.list-+{    deb ht ...

  8. C#操作Office.word(三)

    前面两篇博客讲解了怎么通过程序控制word的生成,包括生成文字.添加表格.添加图片等.这篇博客主要说一下怎么把word图片转换成pdf. using System; using System.Coll ...

  9. beanUtils操作bean的属性

    beanUtils操纵bean属性: 需要jar包commons-beanutils-x.x.x.jar    同时commons-beanutils-x.x.x.jar需要commons-loggi ...

  10. ThinkPHP第十三天(CONF_PATH、APP_PATH,UEditor用法)

    1.CONF_PATH 项目配置文件目录地址,APP_PATH 项目地址 2.ThinkPHP中更新数据的连接操作位save(),更新一个字段可以用setField(name,value)方法. 3. ...