天天看點

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表格中的資料,在讀取資料時直接根據列名去擷取相應的值。

python讀取表格資料_Python讀取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)

得出結果如下:

python讀取表格資料_Python讀取Excel資料并根據列名取值

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-excel.org/ 執行個體: (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.npm install element-tree-grid --save(下載下傳ele ...

RSP小組——團隊沖刺部落格五

RSP小組--團隊沖刺部落格五 沖刺日期:2018年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指向的流 提供了輸入操作符(& ...