[使用Python附加Json文件

问题描述 投票:-2回答:1

我正在尝试在具有以下代码的json文件(test.json)上生成新的key:value对:

{
"computer_specs": [
    {"computer_name": "device1"}
],
"deleted_flag": 0,
"id": "",
"name": "Custom Group-Ops Reporting Remediation"
     }

我在.csv文件(test.csv)上有单独的设备列表,该设备只有一列...多行。

理想情况下,我想从test.csv文件中提取设备列表,并按照以下示例在json文件的“ computer_specs”部分下创建新条目:

{
"computer_specs": [
    {"computer_name": "sql1.tam.local"},
    {"computer_name": "device1"},
    {"computer_name": "device2"},
    {"computer_name": "device3"}
],
"deleted_flag": 0,
"id": "",
"name": "Custom Group-Ops Reporting Remediation"
     }
python json append
1个回答
0
投票

正如我所得到的,您想向computer_spaces添加一个新的dic结构/“ Json”。我建议将csv列表中的“附加”值添加到您的json文件中遵循一段代码,将新字典添加到“ computer_spaces”的值列表中。我假设变量名是test

test['computer_specs'].append({'computer_name':'device5'})

运行此代码时,结构的值会发生变化,例如_

{
"computer_specs": [
    {"computer_name": "sql1.tam.local"},
    {"computer_name": "device1"},
    {"computer_name": "device2"},
    {"computer_name": "device3"},
    {"computer_name": "device5"}
],
"deleted_flag": 0,
"id": "",
"name": "Custom Group-Ops Reporting Remediation"
     }
© www.soinside.com 2019 - 2024. All rights reserved.