如何使用Python在该文件中复制JSON文件的一部分?

时间:2019-03-08 03:24:09

标签: python json

是否可以使用Python在同一个文件中复制JSON文件的一部分?我想使用文件的Changelog部分,并在“视图”部分的 中复制“视图”部分的元素。

这是我要复制的代码:

{
  "title": "1.0",
  "useBoldText": true,
  "useBottomMargin": true,
  "class": "DepictionSubheaderView"
},
{
  "markdown": "\t\n\u2022 Initial Release",
  "useSpacing": false,
  "class": "DepictionMarkdownView"
},
{
  "markdown": "<small style=\"color: #999; margin-top: -8px;\">Released 3/7/2019</small>",
  "useRawFormat": true,
  "class": "DepictionMarkdownView"
}

JSON文件:

{
  "minVersion": "0.1",
  "headerImage": "",
  "tintColor": "",
  "tabs": [
    {
      "tabname": "Changelog",
      "views": [
        {
          "title": "1.0",
          "useBoldText": true,
          "useBottomMargin": true,
          "class": "DepictionSubheaderView"
        },
        {
          "markdown": "\t\n\u2022 Initial Release",
          "useSpacing": false,
          "class": "DepictionMarkdownView"
        },
        {
          "markdown": "<small style=\"color: #999; margin-top: -8px;\">Released 3/7/2019</small>",
          "useRawFormat": true,
          "class": "DepictionMarkdownView"
        }
      ],
      "class": "DepictionStackView"
    }
  ],
  "class": "DepictionTabView"
}

完成后应该看起来像这样:

{
  "minVersion": "0.1",
  "headerImage": "",
  "tintColor": "",
  "tabs": [
    {
      "tabname": "Changelog",
      "views": [

      {
        "title": "1.1",
        "useBoldText": true,
        "useBottomMargin": true,
        "class": "DepictionSubheaderView"
      },
      {
        "markdown": "\t\n\u2022 Some More Changes",
        "useSpacing": false,
        "class": "DepictionMarkdownView"
      },
      {
        "markdown": "<small style=\"color: #999; margin-top: -8px;\">Released 3/7/19</small>",
        "useRawFormat": true,
        "class": "DepictionMarkdownView"
      }
     ],

        {
          "title": "1.0",
          "useBoldText": true,
          "useBottomMargin": true,
          "class": "DepictionSubheaderView"
        },
        {
          "markdown": "\t\n\u2022 Initial Release",
          "useSpacing": false,
          "class": "DepictionMarkdownView"
        },
        {
          "markdown": "<small style=\"color: #999; margin-top: -8px;\">Released 3/1/2019</small>",
          "useRawFormat": true,
          "class": "DepictionMarkdownView"
        }
      ],

      "class": "DepictionStackView"
    }
  ],
  "class": "DepictionTabView"
}

2 个答案:

答案 0 :(得分:0)

您阅读了JSON文件,将需要的部分放入文件的开头。假设您的文件名为data.json

import json

with open('data.json') as f:
    data = json.load(f)

    required_data = data["tabs"][0]["views"]
    f.seek(0, 0)
    f.write(str(required_data) + '\n' + str(data))

答案 1 :(得分:0)

以下代码将打开您的JSON文件,并将JSON结构读入变量data

然后它将创建list.copy(data['tabs'][0]['views'])结构的副本(data['tabs'][0]['views']引用该组索引0的“视图”部分,或者用python术语list引用“标签”部分),在这种情况下,就是您要定位的结构,为变量new_tab

接下来,它将在每个视图的“标题”或“降价” value的{​​{1}}上添加一个空格和单词“ copy”,具体取决于哪个视图,在key(副本)中。

然后将new_tab添加到现有的JSON结构中。

最后,它将更新后的JSON结构写入原始文件。

new_tab

输出将是一个如下所示的文件:

with open(filename, 'r') as copying:
    data = json.load(copying)

new_tab = list.copy(data['tabs'][0]['views'])

for view in new_tab:
    if 'title' in view.keys():
        view['title'] = f"{view['title']} copy"
    elif 'markdown' in view.keys():
        view['markdown'] = f"{view['markdown']} copy"

data['tabs'][0]['views'].extend(new_tab)

with open(filename, 'w') as copying:
    json.dump(data, copying)

另一方面,每当使用JSON时,最好测试一下结构。在JSONLint - The JSON Validator上有大量的资源可用于执行此操作,只需复制JSON结构,将其粘贴到其中,然后单击“验证JSON”按钮即可。