2017-06-20 28 views
3

我是新來的機器人框架和python。我正在使用SudsLibrary開發Web服務。我想從Excel文件讀取數據內容。我爲它編寫了下面的代碼,但它只從文件讀取1行。我希望它讀取文件中的所有行。閱讀機器人框架中的excel內容

Test.robot 

*** Settings *** 
Library DataReader.py 

*** Variables *** 
${file} ${CURDIR}${/}Book2.xls 
${sheet} ABC 

*** Test Cases *** 
Test data provider 
[Setup] prepare data 
Create Soap Client http://test.asmx?WSDL 
${ABC} Create Wsdl Object ABC 
:FOR ${ABC.Col1} ${ABC.Col2} ${ABC.Col3} ${ABC.Col4} ${ABC.Col5}  ${ABC.Col6} ${ABC.Col7} in @{testData} 
\ ${ABC.Col1} Set Variable ${ABC.Col1} 
\ ${ABC.Col2} Set Variable ${ABC.Col2} 
\ ${ABC.Col3} Set Variable ${ABC.Col3} 
\ ${ABC.Col4} Set Variable ${ABC.Col4} 
\ ${ABC.Col4} = convert to integer  ${ABC.Col4} 
\ ${ABC.Col5} Set Variable ${ABC.Col5} 
\ ${ABC.Col6} Set Variable  ${ABC.Col6} 
\ ${ABC.Col6}= convert to integer  ${ABC.Col6} 
\ ${ABC.Col7} Set Variable ${ABC.Col7} 
\ ${ABC.Col7}= convert to integer  ${ABC.Col7} 
\ Set Test Variable ${ABC} 
\ Call Soap Method ABC ${ABC} 
\ ${soap_response} Get Last Received 
\ Log ${soap_response} 
\ Element Text Should Be ${soap_response} 2.991880011689 


*** Keywords *** 
prepare data 
${data}= getDataFromSpreadsheet ${file} ${sheet} 
Set Test Variable ${testData}  ${data} 

DataReader.py

import xlrd 

def getDataFromSpreadsheet(fileName, sheetname) : 
workbook = xlrd.open_workbook(fileName) 
worksheet = workbook.sheet_by_name(sheetname) 
print worksheet 
rowEndIndex = worksheet.nrows - 1 
colEndIndex = worksheet.ncols - 1 
rowStartIndex = 1 
colStartIndex = 0 
testData = [] 
dataRow = [] 

curr_row = rowStartIndex 
while curr_row <= rowEndIndex: 
    cur_col = colStartIndex 
    while cur_col <= colEndIndex: 
     cell_type = worksheet.cell_type(curr_row, cur_col) 

     value = worksheet.cell_value(curr_row, cur_col) 
     dataRow.append(value) 
     cur_col+=1 
    curr_row += 1 
    # testData.append(dataRow) 
# return testData 
return dataRow 

`

+2

你用過熊貓? Excel文件有一個很好的功能[Pandas.read_excel()](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_excel.html) –

+0

我現在可以讀取所有行,現在我必須用csv做同樣的事情。 –

+1

熊貓還具有讀取CSV文件的功能:[read_csv()](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html) – Xukrao

回答

0

爲了通過行從CSV讀取數據行,我使用下面Python代碼。

csvLibrary.py

import csv 
class csvLibrary(object): 

def read_csv_file(self, filename): 
    '''This creates a keyword named "Read CSV File" 

    This keyword takes one argument, which is a path to a .csv file. It 
    returns a list of rows, with each row being a list of the data in 
    each column. 
    ''' 
    data = [] 
    with open(filename, 'rb') as csvfile: 
     reader = csv.reader(csvfile) 
     next(reader,None) 
     for row in reader: 
      for i in row: 
       data.append(i) 
    return data 
0

下面是示例代碼從Excel讀取值文件 -

Open Excel ${CURDIR}/${EXCEL_FILE_NAME} 

${strColCount} = Get Column Count ${EXCEL_SHEET_NAME} 

Log To Console \nCols are => ${strColCount} 

${strRowCount} = Get Row Count ${EXCEL_SHEET_NAME} 

Log To Console \nRows are=> ${strRowCount} 

Set Test Variable ${ROW_ID} 3 

:FOR ${colIndex} IN RANGE 1 ${strColCount} 
    \ ${strTempColValue} Read Cell Data By Coordinates ${EXCEL_SHEET_NAME} ${colIndex} ${ROW_ID} 

可變strTempColValue現在將具有所需的列索引&給定行的值。在這裏,在這個例子中,我們已經給定的行爲3

[注:http://navinet.github.io/robotframework-excellibrary/ExcelLibrary-KeywordDocumentation.html]