python读取excel按列获取设置字符编码

2025-11-15 08:01:22

1、一、安装xlrd库

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

  pip install xlrd

二、使用xlrd读取excel数据

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

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

2、def readExcelDataByName(fileName, sheetName): 

2     table = None

 3     errorMsg = None 

4     try: 

5         data = xlrd.open_workbook(fileName) 

6         table = data.sheet_by_name(sheetName) 

7     except Exception, msg: 

8         errorMsg = msg   9     return table, errorMsg

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

python读取excel按列获取设置字符编码

4、def readExcelDataByIndex(fileName, sheetIndex):

2     table = None

3     errorMsg = ""

4     try:

5         data = xlrd.open_workbook(fileName)

6         table = data.sheet_by_index(sheetIndex)

7     except Exception, msg:

8         errorMsg = msg

9     return table, errorMsg

python读取excel按列获取设置字符编码

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

python读取excel按列获取设置字符编码

6、def getColumnIndex(table, columnName): 2     columnIndex = None 3      3     for i in range(table.ncols): 5         4         if(table.cell_value(0, i) == columnName): 5             columnIndex = i 6             break 7     return columnIndex

python读取excel按列获取设置字符编码

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

python读取excel按列获取设置字符编码

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

1 #!/usr/bin/python 2 # coding=utf-8 3 __author__ = 'Paul' 4 import xlrd 5 import chardet 6 import traceback 7 def getColumnIndex(table, columnName): 8     columnIndex = None 9     #print table10     for i in range(table.ncols):11         #print columnName12         #print table.cell_value(0, i)13         if(table.cell_value(0, i) == columnName):14             columnIndex = i15             break16     return columnIndex17 def readExcelDataByName(fileName, sheetName):18     #print fileName19     table = None20     errorMsg = ""21     try:22         data = xlrd.open_workbook(fileName)23         table = data.sheet_by_name(sheetName)24     except Exception, msg:25         errorMsg = msg26     return table, errorMsg27 def readExcelDataByIndex(fileName, sheetIndex):28     table = None29     errorMsg = ""30     try:31         data = xlrd.open_workbook(fileName)32         table = data.sheet_by_index(sheetIndex)33     except Exception, msg:34         errorMsg = msg35     return table, errorMsg36 if __name__ == '__main__':37     #example38     xlsfile= 'F:/test_AutoTesting/TestCase/RunList.xlsx'39     table = readExcelDataByName(xlsfile, 'Sheet1')[0]40     #获取第一行的值    41     testcase_id = table.cell_value(1, getColumnIndex(table,'TestCaseID'))42     app_config = table.cell_value(1, getColumnIndex(table,'APPConfig'))43 44     print u'测试用例ID为:%s'%(testcase_id)45     print u'配置信息为:%s'%(app_config)

python读取excel按列获取设置字符编码

9、得出结果如下:

python读取excel按列获取设置字符编码

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

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

python读取excel按列获取设置字符编码

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

python读取excel按列获取设置字符编码

12、 以上就是小编带给大家的python读取excel按列获取设置字符编码的关键所在,希望大家可以喜欢,如果喜欢的话可以点赞哦,也可以发表自己的看法 

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢