Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python更新JSON文件中的值_Python_Json - Fatal编程技术网

使用Python更新JSON文件中的值

使用Python更新JSON文件中的值,python,json,Python,Json,如何输入json文件并查找“类”:“descriptionScreenshotsView”,并将其替换为“类”:“?欢迎任何帮助 代码/我尝试的内容: #!/usr/bin/env python3 import json # Load the data file_name = "path/to/json/file" with open(file_name) as fh: full_data = json.load(fh) screen_shots = full_data['tabs'

如何输入json文件并查找
“类”:“descriptionScreenshotsView”
,并将其替换为
“类”:“
?欢迎任何帮助

代码/我尝试的内容:

#!/usr/bin/env python3
import json

# Load the data
file_name = "path/to/json/file"
with open(file_name) as fh:
    full_data = json.load(fh)

screen_shots = full_data['tabs'][0]['views'][3]['screenshots']

for number, screen_shot in enumerate(screen_shots):
    new_url = input("Screnshot URL: ").strip()

    str = """{
        "class" : "DepictionScreenshotsView"
    }"""
    data = json.loads(str)
    data["class"] = "test"

    full_data['tabs'][0]['views'][3]['screenshots'] = screen_shots

with open(file_name, 'w') as fh:
    json.dump(full_data, fh, indent=4)
JSON文件:

{
   "minVersion": "1",
   "class": "DepictionTabView",
   "tintColor": "",
   "headerImage": "",
   "tabs": [
      {
         "tabname": "Details",
         "class": "DepictionStackView",
         "tintColor": "",
         "views": [
            {
               "class": "DepictionSubheaderView",
               "useBoldText": true,
               "useBottomMargin": false,
               "title": "Description"
            },
            {
               "class": "DepictionMarkdownView",
               "markdown": "Some dummy text...",
               "useRawFormat": true
            },
            {
               "class": "DepictionSeparatorView"
            },
            {
               "class": "DepictionSubheaderView",
               "useBoldText": true,
               "useBottomMargin": false,
               "title": "Screenshots"
            },
            {
               "class": "DepictionScreenshotsView",
               "itemCornerRadius": 6,
               "itemSize": "{160, 284.44444444444}",
               "screenshots": [
                  {
                     "accessibilityText": "Screenshot",
                     "url": "http://example.com/image.png"
                  }
               ]
            },
            {
               "class": "DepictionSeparatorView"
            },
            {
               "class": "DepictionSubheaderView",
               "useBoldText": true,
               "useBottomMargin": false,
               "title": "Information"
            },
            {
               "class": "DepictionTableTextView",
               "title": "Author",
               "text": "User"
            },
            {
               "class": "DepictionSpacerView",
               "spacing": 16
            },
            {
               "class": "DepictionStackView",
               "views": [
                  {
                     "class": "DepictionTableButtonView",
                     "title": "Contact",
                     "action": "http://example.com/",
                     "openExternal": true
                  }
               ]
            },
            {
               "class": "DepictionSpacerView",
               "spacing": 16
            }
         ]
      },
      {
         "tabname": "History",
         "class": "DepictionStackView",
         "views": [
            {
               "class": "DepictionSubheaderView",
               "useBoldText": true,
               "useBottomMargin": false,
               "title": ""
            },
            {
               "class": "DepictionMarkdownView",
               "markdown": "",
               "useRawFormat": true
            }
         ]
      }
   ]
}
编辑:我也尝试了这个代码,但没有运气(顺便说一句,这只是我的代码剪短,而不是完整的代码)


IIUC,您应该能够循环到嵌套元素中,以找到您想要的:

导入json
将open(“/path/to/file.json”)作为fh:
content=json.load(fh)
#get中的第二个参数是在密钥不存在时返回的值
#并且返回空列表将防止出现错误,说明NoneType不可编辑
对于content.get中的选项卡('tabs',[]):
对于content.get中的视图('views',[]):
如果view.get('class')='descriptionScreenshotsView':
视图['class']=''
#这将在适当的位置修改内容
将open('/path/to/newfile.json',w')作为fh:
dump(content,fh,indent=4)
这将提供:

{
    "minVersion": "1",
    "class": "DepictionTabView",
    "tintColor": "",
    "headerImage": "",
    "tabs": [
        {
            "tabname": "Details",
            "class": "DepictionStackView",
            "tintColor": "",
            "views": [
                {
                    "class": "DepictionSubheaderView",
                    "useBoldText": true,
                    "useBottomMargin": false,
                    "title": "Description"
                },
                {
                    "class": "DepictionMarkdownView",
                    "markdown": "Some dummy text...",
                    "useRawFormat": true
                },
                {
                    "class": "DepictionSeparatorView"
                },
                {
                    "class": "DepictionSubheaderView",
                    "useBoldText": true,
                    "useBottomMargin": false,
                    "title": "Screenshots"
                },
                {
                    "class": "",
                    "itemCornerRadius": 6,
                    "itemSize": "{160, 284.44444444444}",
                    "screenshots": [
                        {
                            "accessibilityText": "Screenshot",
                            "url": "http://example.com/image.png"
                        }
                    ]
                },
                {
                    "class": "DepictionSeparatorView"
                },
                {
                    "class": "DepictionSubheaderView",
                    "useBoldText": true,
                    "useBottomMargin": false,
                    "title": "Information"
                },
                {
                    "class": "DepictionTableTextView",
                    "title": "Author",
                    "text": "User"
                },
                {
                    "class": "DepictionSpacerView",
                    "spacing": 16
                },
                {
                    "class": "DepictionStackView",
                    "views": [
                        {
                            "class": "DepictionTableButtonView",
                            "title": "Contact",
                            "action": "http://example.com/",
                            "openExternal": true
                        }
                    ]
                },
                {
                    "class": "DepictionSpacerView",
                    "spacing": 16
                }
            ]
        },
        {
            "tabname": "History",
            "class": "DepictionStackView",
            "views": [
                {
                    "class": "DepictionSubheaderView",
                    "useBoldText": true,
                    "useBottomMargin": false,
                    "title": ""
                },
                {
                    "class": "DepictionMarkdownView",
                    "markdown": "",
                    "useRawFormat": true
                }
            ]
        }
    ]
}

它看起来还保留了顺序,尽管我不能说这是否是一种保证,因为我以前从未注意过这种行为,所以您应该能够循环到嵌套元素中以找到您想要的:

导入json
将open(“/path/to/file.json”)作为fh:
content=json.load(fh)
#get中的第二个参数是在密钥不存在时返回的值
#并且返回空列表将防止出现错误,说明NoneType不可编辑
对于content.get中的选项卡('tabs',[]):
对于content.get中的视图('views',[]):
如果view.get('class')='descriptionScreenshotsView':
视图['class']=''
#这将在适当的位置修改内容
将open('/path/to/newfile.json',w')作为fh:
dump(content,fh,indent=4)
这将提供:

{
    "minVersion": "1",
    "class": "DepictionTabView",
    "tintColor": "",
    "headerImage": "",
    "tabs": [
        {
            "tabname": "Details",
            "class": "DepictionStackView",
            "tintColor": "",
            "views": [
                {
                    "class": "DepictionSubheaderView",
                    "useBoldText": true,
                    "useBottomMargin": false,
                    "title": "Description"
                },
                {
                    "class": "DepictionMarkdownView",
                    "markdown": "Some dummy text...",
                    "useRawFormat": true
                },
                {
                    "class": "DepictionSeparatorView"
                },
                {
                    "class": "DepictionSubheaderView",
                    "useBoldText": true,
                    "useBottomMargin": false,
                    "title": "Screenshots"
                },
                {
                    "class": "",
                    "itemCornerRadius": 6,
                    "itemSize": "{160, 284.44444444444}",
                    "screenshots": [
                        {
                            "accessibilityText": "Screenshot",
                            "url": "http://example.com/image.png"
                        }
                    ]
                },
                {
                    "class": "DepictionSeparatorView"
                },
                {
                    "class": "DepictionSubheaderView",
                    "useBoldText": true,
                    "useBottomMargin": false,
                    "title": "Information"
                },
                {
                    "class": "DepictionTableTextView",
                    "title": "Author",
                    "text": "User"
                },
                {
                    "class": "DepictionSpacerView",
                    "spacing": 16
                },
                {
                    "class": "DepictionStackView",
                    "views": [
                        {
                            "class": "DepictionTableButtonView",
                            "title": "Contact",
                            "action": "http://example.com/",
                            "openExternal": true
                        }
                    ]
                },
                {
                    "class": "DepictionSpacerView",
                    "spacing": 16
                }
            ]
        },
        {
            "tabname": "History",
            "class": "DepictionStackView",
            "views": [
                {
                    "class": "DepictionSubheaderView",
                    "useBoldText": true,
                    "useBottomMargin": false,
                    "title": ""
                },
                {
                    "class": "DepictionMarkdownView",
                    "markdown": "",
                    "useRawFormat": true
                }
            ]
        }
    ]
}

它看起来还保留了顺序,尽管我不能说这是否是一种保证,因为我以前从未注意过这种行为

您关心保留json的格式吗?使用
json
库最简单的方法是对文档重新排序,并将其作为一行写出来can@PeterGibson带有
缩进的
json.dump
kwarg将转储到带有换行符和indents@Unknown关于您所使用的代码,有什么不起作用张贴?有错误吗?
Traceback(最近一次调用):full_data=json.load(fh)*此处有更多错误,以列出所有*从None json.decoder.jsondeCoderror中引发JSondeCoderror(“预期值”,s,err.value”):预期值:第1行第1列(char 0)
您关心保留json的格式吗?使用
json
库最简单的方法是对文档重新排序,并将其作为一行写出来can@PeterGibson带有
缩进的
json.dump
kwarg将转储到带有换行符和indents@Unknown关于您所使用的代码,有什么不起作用张贴?有错误吗?
Traceback(最近一次调用):full_data=json.load(fh)*此处有更多错误,以列出所有*从None json.decoder.jsondeCoderror中提出JSondeCoderror(“期望值”,s,err.value”):期望值:第1行第1列(char 0)
似乎不起作用。这是代码:(代码只是有点剪短,不可能是我的完整代码)我无法访问该站点它似乎不起作用这是代码:(代码只是有点剪短,不可能是我的完整代码)我无法访问该站点