簡體   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