首页 > 解决方案 > 字典字典:TypeError:列表索引必须是整数,而不是 unicode

问题描述

我的目标是拥有一个包含字符串作为键和其他字典作为值的字典。

所以我构建了下面的代码来迭代所有值。

但是,我面临错误:TypeError: list indices must be integers, not unicode

*** Test Cases ***
...
:FOR    ${element}    IN    @{elements}
\    ${elementDictionary}=    Create Dictionary
\    Set To Dictionary    ${elementDictionary}    k1    ${v1}
\    Set To Dictionary    ${elementDictionary}    k2    ${v2}
\    Set To Dictionary    ${elementDictionary}    k3    ${v3}
\    Set To Dictionary    ${elementsDictionary}    ${k}    ${elementDictionary}
END

:FOR    ${elementKey}    IN    @{elementsDictionary}
\    @{element}=    Get From Dictionary    ${elementsDictionary}    ${elementKey}
\    Browse Dictionary Value    @{element}
\    Log To Console      ----------------------
END

*** Keywords ***
Browse Dictionary Value
    [Arguments]    @{dictionary}
    :FOR    ${key}    IN    @{dictionary}
    \    ${value}=    Get From Dictionary    ${dictionary}    ${key}
    \    Log To Console    ${key}    
    \    Log To Console    ${value}
    END

错误发生在以下行:${value}= Get From Dictionary ${dictionary} ${key}

请你帮助我好吗?十分感谢。

解决方案

用来${element}代替@{element}

标签: pythonrobotframework

解决方案


用于Get Dictionary Keys获取字典键并循环该列表而不是@{dictionary}Browse Dictionary Value关键字。


推荐阅读