gpt4 book ai didi

python - 我在机器人框架中得到了元组列表而不是字典

转载 作者:太空宇宙 更新时间:2023-11-03 18:00:54 24 4
gpt4 key购买 nike

我想读取一个excel文件,所以我在robot框架中写了这些关键字:

*** 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
==============================================================================

最佳答案

从 Excel 读取数据调用获取行值,而不是获取行的所有值,后者会创建字典。 获取行的所有值未在提供的代码中使用。

也许您认为从 Excel 读取数据正在使用您的用户关键字,而实际上它正在使用库关键字。

关于python - 我在机器人框架中得到了元组列表而不是字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27703849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com