繁体   English   中英

在 Python/RobotFramework 我应该如何将 Append 所需的键、嵌套 json 中的值放入另一个文件中?

[英]In Python/RobotFramework How Should I Append the required keys, values from nested json into another file?

我有一个嵌套的 JSON 格式如下,我需要从中获取标签名称和值以及 append 如果重复出现在另一个 json 文件中。

${resp}= {
"data": {
"resources": {
"edges": [
{
"node": {
    "tags": [],
   }
},
{          
  "node": {
   "tags": [
      {
        "name": "app",
        "value": "e2e"
      },
      {
        "name": "Cost",
         "value": "qwerty"
      }
  }
},
{          
  "node": {
   "tags": [
      {
        "name": "app",
        "value": "e2e"
      },
      {
        "name": "Cost",
         "value": "qwerty"
      },
        {
        "name": "test",
         "value": "qwerty"
      }
  }
}
]
}
}
}

我需要单独获取标签键和值以及 append 并将其存储在 json 文件中。 请参阅下面我尝试过的 Python 代码。

Python代码:

def appendjsondata(fileName,data):
 new = {}
 print (data)
 print('forloop before')
 for k,v in data.items():

    print(f'{k}: {v}')
    new["key"] = k
    new["tags"] = []
    new["value"] = v

    #new["tags"].append([{ 'key': k, 'values': v } for k, v in data.items()])
    new["tags"].append(new)
 print(new["tags"])
 with open(fileName, 'w') as f:
   json.dump(new["tags"], f, indent=3 * ' ')  

 return  new["tags"]

机器人框架代码:

*** Settings ***
Library   pythonfile.py
Library   JSONLibrary
Library   Collections

*** Test Cases ***
${dict1}=        Set Variable  ${resp}
${cnt}=     get length     ${dict1['data']['resources']['edges']}
${edge}=   set variable      ${dict1['data']['resources']['edges']}

run keyword if   ${cnt}==0     set test message    The resources count is Zero(0)
log to console  ${cnt}-count

: FOR    ${item}    IN RANGE   0    ${cnt}
\    ${readName}=    Set Variable     ${edge[${item}]['node']['configuration']}
\    ${tag_Count}=    get length     ${edge[${item}]['node']['tags']}
\    ${tag_variable}=   set variable   ${edge[${item}]['node']['tags']}
\    forkeyword       ${tag_Count}   ${tag_variable}    ${readName}

 ${req_json}    Json.Dumps    ${dict}
 Create File  results.json  ${req_json}


forkeyword
[Arguments]       ${tag_Count}      ${tag_variable}     ${readName}
@{z}=   create list
: FOR    ${item}    IN RANGE   0    ${tag_Count}
         \   ${resourceName}=    run keyword if     ${tag_Count} > 0   set variable    ${readName['name']}
         \   log to console  ${resourceName}-forloop
         \   ${readkey}=     set variable   ${tag_variable[${item}]['name']}
         \   ${readvalue}=     set variable   ${tag_variable[${item}]['value']}
         \   set to dictionary     ${dict}     resourceName   ${resourceName}
         \   set to dictionary  ${dict}    ${readkey}     ${readvalue}
         \    appendjsondata      results.json    ${dict}
set suite variable ${dict}

错误:

未找到关键字 appendjsondata

机器人文件无法识别 python 程序中定义的关键字。 检查以下步骤:

1.检查python程序没有编译问题,导入正确。 控制台将显示错误消息。

  1. 在 python class 定义中添加 ROBOT_LIBRARY_SCOPE = 'TEST SUITE'。

库导入中只有一个空格。 在导入中使用两个或多个空格,例如Library pythonfile.py

并检查pythonfile.py是否与 RF 文件位于同一目录中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM