[英]Robot Framework: Merge and sum values of dictionaries with matching items in a list of dictionaries
[英]Extract all values for a particular Key in a list of dictionaries - using robot framework
在使用机器人框架的字典下面的列表中,我没有得到如何提取键“名称”的所有值的信息。 请帮帮我。
{'data': [{'attributes': {'name': 'Acura'},
'id': 'de3fe2a2-eaa1-45d8-8d6b-298e0b2d0666',
'type': 'brands'},
{'attributes': {'name': 'Alfa Romeo'},
'id': '52b33ad8-71ab-4fdb-b877-a6080e86439c',
'type': 'brands'},
{'attributes': {'name': 'Audi'},
'id': '5f627b7a-193a-40a0-9db7-b8b504177435',
'type': 'brands'},
{'attributes': {'name': 'BMW'},
'id': 'dad04367-2248-45b3-b523-0be7cc7bfbad',
'type': 'brands'},
{'attributes': {'name': 'Buick'},
'id': '64f77989-3007-41f5-b5c4-8cb5f4a25d45',
'type': 'brands'},
{'attributes': {'name': 'Cadillac'},
'id': 'a457ba4e-5f74-4b9a-9316-d26962284983',
'type': 'brands'},
{'attributes': {'name': 'Chevrolet'},
'id': '118e4876-993d-4ad0-91f4-ae3d25b1e5f5',
'type': 'brands'}],
'meta': {'total': 48}}
你可以做这样的事情,
>>> [ i['attributes']['name'] for i in a['data']]
['Acura', 'Alfa Romeo', 'Audi', 'BMW', 'Buick', 'Cadillac', 'Chevrolet']
其中a
是字典的名称。
令d
为字典的名称。 可以使用以下代码提取名称:
names = [a['attributes']['name'] for a in d['data']]
结果:
print names
[u'Acura', u'Alfa Romeo', u'Audi', u'BMW', u'Buick', u'Cadillac', u'Chevrolet']
在这个SO问题中 ,当引用常规Python字典时会出现点表示法问题。 通过将常规Python Dict转换为机器人特定的DotDict可以解决此问题。
另外,如果上述数据是静态的,则可以考虑从Yaml文件导入此数据 ,这也将允许您使用机械手点表示法。
关于循环和获取名称,然后假设将字典分配给变量D并存储在名为dict.py
的文件中,则将允许您获取名称:
*** Settings ***
Variables dict.py
*** Test Cases ***
test
Log To Console \n
:FOR ${node} IN @{d['data']}
\ Log To Console ${node['attributes']['name']}
这将导致以下输出:
test
Acura
Alfa Romeo
Audi
BMW
Buick
Cadillac
Chevrolet
| PASS |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.