1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python读取表格数据_Python读取Excel数据并根据列名取值

python读取表格数据_Python读取Excel数据并根据列名取值

时间:2024-07-04 03:07:08

相关推荐

python读取表格数据_Python读取Excel数据并根据列名取值

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

最近测试过程中需要用到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数据并将测试结果填入Excel

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

Python读取excel 数据

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

Python读取Excel数据

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

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

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

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

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

python 读取excel数据

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

Python 读取Excel数据 xlrd

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

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

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

Selenium2+python自动化之读取Excel数据(xlrd)

前言 当登录的账号有多个的时候,我们一般用excel存放测试数据,本节课介绍,python读取excel方法,并保存为字典格式. 一.环境准备 1.先安装xlrd模块,打开cmd,输入pip inst ...

随机推荐

如何:对 SharePoint 列表项隐藏 ECB 中的菜单项

可以通过使用功能框架向编辑控制块 (ECB) 菜单添加新的自定义操作.但是,您不能使用此方法进行相反的操作,即隐藏现有的 ECB 菜单项,因为它们是通过使用 ECMAScript(JavaScript ...

ajax实现无刷新上传附件并且显示进度条的实例

首先:得把php.ini中的post_max_size和upload_max_filesize改成200M或更大(进度条好看效果,默认是2M) html和js代码: ..

【POJ1823】【线段树】Hotel

Description The "Informatics" hotel is one of the most luxurious hotels from Galaciuc. A l ...

reg 正则

//转化为camel形式 var text = 'border-color-base'; text.replace(/-(\w{1})/g, function (match, chr1) { retu ...

Spring MVC如何测试Controller(使用springmvc mock测试)

在springmvc中一般的测试用例都是测试service层,今天我来演示下如何使用springmvc mock直接测试controller层代码. 1.什么是mock测试? mock测试就是在测试过 ...

element-tree-grid(表格树)的使用

表格树,element-tree-grid需要单独下载并再配合elementUi里el-table使用. 步骤:1.npminstallelement-tree-grid--save(下载ele ...

RSP小组——团队冲刺博客五

RSP小组--团队冲刺博客五 冲刺日期:12月17日 前言 周末的结束,我们并没有完全的休息,对于这个项目,以我们的实力还是需要花费更多的时间. 各成员今日(12.17)完成的任务 马瑞蕃由 ...

opencv人脸检测,旋转处理

年会签到,拍自己的大头照,有的人可能会拍成横向的,需要旋转,用人脸检测并修正它(图片). 1. 无脑检测步骤为: 1. opencv 读取图片,灰度转换 2. 使用CascadeClassifier( ...

【托业】【新托业TOEIC新题型真题】学习笔记3-题库二->P5-6

--------------------------------------单词-------------------------------------- oppose vt. 反对:对抗,抗争 v ...

istream_iterator和ostream_iterator

总结: istream_iteratorin(strm);T指明此istream_iterator的输入类型,strm为istream_iterator指向的流 提供了输入操作符(& ...

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。