Python讀取Excel數據並根據列名取值


一直想將自己接觸到的東西梳理一遍,可就是邁不出第一步,希望從這篇總結開始不要再做行動的矮人了。

最近測試過程中需要用到python讀取excel用例數據,於是去了解和學習了下xlrd庫,這里只記錄使用過程中讀取excel數據相關操作。

一、安裝xlrd庫

  可以下載xlrd庫包到本地安裝,也可以通過pip命令安裝,這里我選擇pip命令:

  pip install xlrd

二、使用xlrd讀取excel數據

  具體詳細的操作可以參考xlrd庫操作說明文檔,以下是兩種讀取excel數據的方法:

  1、根據Excel中sheet名稱讀取數據:    

 1 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

  2、根據Excel中sheet的序號獲取:

1 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

  3、根據列名獲取相應序號,由於有時讀取excel中列數據時,需要通過列頭名稱獲取相應的列中的值,所以寫了下面這個返回列名所在表格中的index。然后就可以直接通過table.cell_value(i, getColumnIndex(table,'列名'))獲取列的值。

 1 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

  下面加入需要讀取如下excel表格中的數據,在讀取數據時直接根據列名去獲取相應的值。

根據列名讀取相應的值,代碼如下:

 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 table
10     for i in range(table.ncols):
11         #print columnName
12         #print table.cell_value(0, i)
13         if(table.cell_value(0, i) == columnName):
14             columnIndex = i
15             break
16     return columnIndex
17 def readExcelDataByName(fileName, sheetName):
18     #print fileName
19     table = None
20     errorMsg = ""
21     try:
22         data = xlrd.open_workbook(fileName)
23         table = data.sheet_by_name(sheetName)
24     except Exception, msg:
25         errorMsg = msg
26     return table, errorMsg
27 def readExcelDataByIndex(fileName, sheetIndex):
28     table = None
29     errorMsg = ""
30     try:
31         data = xlrd.open_workbook(fileName)
32         table = data.sheet_by_index(sheetIndex)
33     except Exception, msg:
34         errorMsg = msg
35     return table, errorMsg
36 if __name__ == '__main__':
37     #example
38     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)

得出結果如下:

  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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM