python - 使用 Python 更新 JSON 文件中的值
问题描述
如何输入 json 文件并查找"class": "DepictionScreenshotsView"
并替换为"class": ""
?欢迎任何帮助。
代码/我试过的:
#!/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
}
]
}
]
}
编辑:我也试过这段代码,但没有运气(顺便说一句,这只是我的代码的一个片段,而不是完整的代码)
import json
# Load the data
file_name = "path/to/json/file"
with open(file_name) as fh:
full_data = json.load(fh)
for tab in full_data.get('tabs', []):
for view in full_data.get('views', []):
if view.get('class') == 'DepictionScreenshotsView':
view['class'] = ''
with open(file_name, 'w') as fh:
json.dump(full_data, fh, indent=4)
解决方案
IIUC,您应该能够循环进入嵌套元素以找到您想要的内容:
import json
with open("/path/to/file.json") as fh:
content = json.load(fh)
# The second arg in get is the return in case the key isn't there
# and returning empty list will prevent errors saying NoneType isn't iterable
for tab in content.get('tabs', []):
for view in content.get('views', []):
if view.get('class') == 'DepictionScreenshotsView':
view['class'] = ''
# This modifies content in place
with open('/path/to/newfile.json', 'w') as fh:
json.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
}
]
}
]
}
看起来订单也被保留了,虽然我不能说这是否是保证,因为我以前从未注意过这种行为
推荐阅读
- excel - 在这种情况下如何获得总利润?
- c# - 任务激活 HighVersionLie 失败
- excel - 如何将每个条形图顶部的 % 更改为数字?
- flutter - 使用 Material () 或 Scaffold () 小部件时,使用不透明度设置颜色样式不起作用
- flutter - 在 Java 中加密和在 dart Flutter 中解密
- flutter - 如何在颤动的列表视图中对多个表单实现表单验证?
- python - 从几年的时间序列中删除一天中的某些小时v - python
- cordova - 科尔多瓦插件是否与电容器(离子)一起使用
- mysql - Windows 更新后 Xammp MySQL 无法启动
- google-bigquery - 根据另一个表中的开始日期和结束日期对值求和