我想阅读一个excel文件,所以我在机器人框架中写了这些关键字:
*** Settings ***
Library ExcelLibrary
Library Collections
Resource ../Variables/ConstantVariables.robot
Resource ../Variables/ConfigVariables.robot
*** Variables ***
*** Keywords ***
Read Data From Excel
[Arguments] ${filename} ${sheetname}
open excel ${filename}
@{excel_result} = Create List
${rows} = get_row_count ${sheetname}
:FOR ${rindex} IN RANGE 1 ${rows}
\ ${ret_val} = Get Row Values ${sheetname} ${rindex}
\ Append To List ${excel_result} ${ret_val}
[Return] ${excel_result}
Get All Values of the Row
[Arguments] ${sheetname} ${rowindex}
${cols} = get_column_count ${sheetname}
${vals} = Create Dictionary
${titles} = get_row_values ${sheetname} 0
:FOR ${cindex} IN RANGE 0 ${cols}
\ ${val} = read_cell_data_by_coordinates ${sheetname} ${cindex} ${rowindex}
\ Set To Dictionary ${vals} ${titles[0]} ${val}
[Return] ${vals}
这是方案文件:
*** Settings ***
Resource ../Variables/ConstantVariables.robot
Resource ../Variables/ConfigVariables.robot
Resource ../Keywords/CommonKeywords.robot
*** Test Cases ***
Test For Loop
@{ret_val} = Read Data From Excel ${jobfilename} ${jobsheetname}
log to console ${ret_val}
这是我得到的输出:
PS D:\Abbasimazar\workspace\RobotTests\Scenarios> pybot .\scenario.robot
==============================================================================
Scenario
==============================================================================
Test For Loop .[[('A2', u't1'), ('B2', u'n1'), ('C2', u'c1')], [
('A3', u't2'), ('B3', u'n2'), ('C3', u'c2')], [('A4', u't3'), ('B4', u'n3'), ('C4', u'c3')]]
Test For Loop | PASS |
------------------------------------------------------------------------------
Scenario | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Output: D:\Abbasimazar\workspace\RobotTests\Scenarios\output.xml
Log: D:\Abbasimazar\workspace\RobotTests\Scenarios\log.html
Report: D:\Abbasimazar\workspace\RobotTests\Scenarios\report.html
但我希望得到这个:
[{'A2': u't1', 'B2': u'n1', 'C2': u'c1'}, {'A3': u't2', 'B3': u'n2', 'C3': u'c2'}, {'A4': u't3', 'B4': u'n3', 'C4': u'c3'}]
任何想法我做错了什么?
编辑1:
正如@ ombre42所说,调用关键字时出错。我应该换行:
${ret_val} = Get Row Values ${sheetname} ${rindex}
成:
${ret_val} = Get All Values of the Row ${sheetname} ${rindex}
但在那之后我注意到了这一行
${titles} = get_row_values ${sheetname} 0
给了我这个:
==============================================================================
Scenario
==============================================================================
Test For Loop .[{('A1', u'test'): u'c1'}, {('A1', u'test'): u'c2
'}, {('A1', u'test'): u'c3'}]
Test For Loop | PASS |
------------------------------------------------------------------------------
Scenario | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
所以我写了新方法来获得标题:
Get Titles From Sheet
[Arguments] ${sheetname}
${cols} = get_column_count ${sheetname}
@{titles} = Create List
:FOR ${cindex} IN RANGE 0 ${cols}
\ ${val} = read_cell_data_by_coordinates ${sheetname} ${cindex} 0
\ Append To List ${titles} ${val}
[Return] ${titles}
并将该行更改为:
${titles} = Get Titles From Sheet ${sheetname}
现在结果是我想要的:
==============================================================================
Scenario
==============================================================================
Test For Loop .[{u'test': u't1', u'name': u'n1', u'class': u'c1'
}, {u'test': u't2', u'name': u'n2', u'class': u'c2'}, {u'test': u't3', u'name': u'n3', u'class': u'c3'}]
Test For Loop | PASS |
------------------------------------------------------------------------------
Scenario | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
答案 0 :(得分:1)
从Excel读取数据调用获取行值而不是获取行的所有值,这将创建字典。 获取行的所有值未在提供的代码中使用。
也许您认为从Excel读取数据正在使用您的用户关键字,而实际上它正在使用库关键字。
答案 1 :(得分:0)
也许是OrderedDict? OrderedDicts表示看起来像元组列表:
>>> from collections import OrderedDict
>>> d = OrderedDict()
>>> d['x'] = 1
>>> d
OrderedDict([('x', 1)])
即使它是元组的常规列表而不是OrderedDict,将它们转换为dict也是微不足道的,因为dict构造函数可以直接获取元组列表:
>>> dict([('x', 1)])
{'x': 1}