簡體   English   中英

如何從嵌套字典中獲取值?

[英]How to get values from nested dict?

我是機器人和 Python 的新手。 我有一個嵌套字典,我需要從中獲取值,但我無法訪問它們。

${result} =  Get Info  from_here=location  mode=aggregate
@{keys}=    Get Dictionary Keys    ${result}
${robot_dict}=    Create Dictionary
:FOR    ${key}    IN    @{keys}
    \    Set To Dictionary    ${robot_dict}    ${key}=${result['${key}']}
Log Many  ${robot_dict}
######Output from robot_dict is:
#{'Name1': 
#       {'nickname': 
#           {'Name': 'Name1', 'FirstName': 'Full', 'Boy': 
#               {'Age': 'N/A', 'Single': '0'}, 
#               'Girl': 
#               {'Height': '229057', 'Weight': 'N/A'}
#           }
#       }, 
#'Name2':{'nickname': 
#           {'Name': 'Name2', 'FirstName': 'Full', 'Boy': 
#               {'raw_pkt_count': '229059', 'Age': 'N/A'}, 
#               'Girl': 
#               {'Height': '0', 'Weight': 'N/A'}
#           }
#       }
#}
#############

試過這個:

${current_key} =  Get Dictionary Keys  ${robot_dict[Name1][nickname][Girl][Height]}       
######Error: Resolving variable '${robot_dict[Name1][nickname][Girl][Height]}' failed: KeyError: 0         

和這個:

${current_key} =  Get Dictionary Keys  ${robot_dict}    Name1.nickname.Girl.Height   
#######Error: Dictionary does not contain key Name1.nickname.Girl.Height

當您使用擴展變量語法時,您必須將代碼視為 Python。 由於鍵是字符串,因此需要正確引用它們。 這是文檔中的相關部分:

  1. 大括號內的表達式被計算為 Python 表達式,因此基本變量名稱被替換為其值。 如果評估由於無效語法或查詢的屬性不存在而失敗,則會引發異常並且測試失敗。
${current_key} =  Get Dictionary Keys  ${robot_dict['Name1']['nickname']['Girl']['Height']}       

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM