Python Can';Robotframework中带点符号的t-access字典
我有这样的嵌入式词典:Python Can';Robotframework中带点符号的t-access字典,python,python-2.7,dictionary,robotframework,Python,Python 2.7,Dictionary,Robotframework,我有这样的嵌入式词典: ${json}= Create Dictionary ......(value pairs) ${request}= Create Dictionary body=${json} ${return}= Create Dictionary request=${request} ${decoded_json}= Run Keyword And Ignore Error json.JSONDecoder.Decode ${response.cont
${json}= Create Dictionary ......(value pairs)
${request}= Create Dictionary body=${json}
${return}= Create Dictionary request=${request}
${decoded_json}= Run Keyword And Ignore Error json.JSONDecoder.Decode ${response.content}
${response.json}= Set Variable If '${decode_status}' == 'PASS' ${decoded_json} ${null}
${detail_call}= Create Dictionary response=${response}
我可以访问原始键值对,例如:
${return.request.body.type}
一切都很好。但是,我得到了这个错误:
Resolving variable '${detail_call.response.json.type}' failed: AttributeError: 'dict' object has no attribute 'type'
当我尝试创建初始json对象时,反之亦然-通过从字符串中解码它,如下所示:
${json}= Create Dictionary ......(value pairs)
${request}= Create Dictionary body=${json}
${return}= Create Dictionary request=${request}
${decoded_json}= Run Keyword And Ignore Error json.JSONDecoder.Decode ${response.content}
${response.json}= Set Variable If '${decode_status}' == 'PASS' ${decoded_json} ${null}
${detail_call}= Create Dictionary response=${response}
并通过点符号访问它:
${detail_call.response.json.type}
当我记录字符串时,我可以清楚地看到有一个带有赋值的键“type”。当我使用括号访问它时,它甚至可以工作:
${detail_call.response.json['type']}
你知道吗,如果字典是用JSONDecoder创建的,为什么我不能使用点符号
谢谢。正如millimoose所建议的,JSONDecoder返回的python字典与robotframework自己的字典不同。我还没有找到将python dict转换为robot dict的正式方法,因此我实现了自己的:
Convert Python Dictionary
[Arguments] ${python_dict}
[Documentation] Converts Python dictionary to Robot dictionary.
@{keys}= Get Dictionary Keys ${python_dict}
${robot_dict}= Create Dictionary
:FOR ${key} IN @{keys}
\ Set To Dictionary ${robot_dict} ${key}=${python_dict['${key}']}
[Return] ${robot_dict}
如果您需要将robot dict转换为python dict,您可以使用集合库中的关键字。我被这个问题弄糊涂了。首先,您不能使用Python中的点表示法访问字典(特别是dict),除非您使用的是不同的字典实现允许这样做。点表示法用于对象的属性,但是
jsondeconder
返回dict
s,因此使用括号访问它们的键是完全正常的。另外,您的示例代码似乎不是有效的Python,这是Robot框架特有的语法吗?我想您已经搞定了,我想这确实是不同的dict实现。我不太熟悉python,只熟悉robot,在它的语法中,可以用点访问dict。完全有可能robot只创建简单的对象,而不是字典,因为在python中动态设置属性其实并不难。是的,谢谢你的帮助:)