机器人框架中的 for 循环字典 [英] For loop over dictionary in Robot Framework
问题描述
是否有正确的方法在 RF 中循环字典?我使用pythonic方式,但失败了:
Is there a proper way to loop over a dictionary in RF? I used pythonic way, but failed:
:FOR ${key} ${value} IN &{dict}
输出:FOR 循环值的数量应该是其变量的倍数.有 2 个变量,但有 1 个值.
output: Number of FOR loop values should be multiple of its variables. Got 2 variables but 1 value.
同样,当我将字典指向标量变量时.不过,我在文档中找不到示例.有人解决了吗?
Same, when i pointed dictionary as scalar variable. I couldn't find an example in documentation though. Has anyone solved that?
附言
我知道您使用 kw 的解决方法.获取字典键和获取字典值,然后使用 Set To Dictionary ${key} ${new_value} 更新值,但这似乎对人类不友好,并且使用了多个 for 循环迭代而不是一个.
I am aware of workaround solution, that you use kw. Get Dictionary Keys and Get Dictionary Values, then, to update values you use Set To Dictionary ${key} ${new_value}, but this seems to be human unfriendly and uses several for loops iterations instead one.
推荐答案
Loop Through Dict
&{mydict} Create Dictionary a=1 b=2
:FOR ${key} IN @{mydict.keys()}
\ Log ${mydict["${key}"]}
Loop Through Dict And Multiplicate Values
&{mydict} Create Dictionary a=1 b=2
:FOR ${key} IN @{mydict.keys()}
\ ${new_value} Evaluate ${mydict["${key}"]}*2
\ Set To Dictionary ${mydict} ${key}=${new_value}
Log ${mydict}
这篇关于机器人框架中的 for 循环字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!