我有2500行JSON代码,我需要在其中用新值替换几个值(20位ID)。这是一个快速查找和替换的功能,而且由于有了这个论坛,我昨天才开始工作。 现在,我注意到在python控制台中打印出的代码不是正确的JSON,因此当我复制并粘贴它时,会引发很多错误。我试图找到一种加载JSON文件,运行查找并替换并将结果导出到新JSON文件的方法。
我首先尝试用open加载JSON文件并将其分配给变量,但是当我这样做时,find and replace函数不起作用。因此,我只是将原始代码分配给了一个变量。 但是,当我尝试编写/导出JSON文件时,它根本无法工作。我尝试了很多在网上找到的解决方案,但是所有的抛出错误都没有找到'str'或'write'对象,或者只是创建了空文件。
下面的代码是我需要做的一个例子。我不允许发布实际的代码。
import string
import csv
import json
from pprint import pprint
# These values need to be replaced in the code
# 1) Original View
# 2) View Source
valuesToReplace = ["Original View",
"View Source"
]
# Replace/change these values
newValues = ["New Original View",
"New View Source"
]
data = """{"menu": {
"header": "SVG Viewer",
"items": [
{"id": "Open"},
{"id": "OpenNew", "label": "Open New"},
null,
{"id": "ZoomIn", "label": "Zoom In"},
{"id": "ZoomOut", "label": "Zoom Out"},
{"id": "OriginalView", "label": "Original View"},
null,
{"id": "Quality"},
{"id": "Pause"},
{"id": "Mute"},
null,
{"id": "Find", "label": "Find..."},
{"id": "FindAgain", "label": "Find Again"},
{"id": "Copy"},
{"id": "CopyAgain", "label": "Copy Again"},
{"id": "CopySVG", "label": "Copy SVG"},
{"id": "ViewSVG", "label": "View SVG"},
{"id": "ViewSource", "label": "View Source"},
{"id": "SaveAs", "label": "Save As"},
null,
{"id": "Help"},
{"id": "About", "label": "About Adobe CVG Viewer..."}
]
}}
"""
# If, instead, JSON file is loaded and assigned to variable, ReplaceValues throws an error
# with open(r"C:\jsonexample\codeExample.json") as dataFile:
# data = json.load(dataFile)
def ReplaceValues(valuesToReplace, newValues, data):
for i in range(len(newValues)):
data = data.replace(valuesToReplace[i], newValues[i])
return data
data = ReplaceValues(valuesToReplace, newValues, data)
pprint(data)
json.dump(data, r"C:\\jsonexample\\template\\newCode.json")
答案 0 :(得分:1)
您似乎缺少以r
和w
模式打开文件的信息(更多信息here)。甚至json.dump()
都需要保存文件内容。
示例:
with open('out.json', 'w') as file:
file.write(json.dumps(json_data))
但是对于您而言,您可能根本不需要将其读取为JSON,因为它是通用查找,并且全部替换了所有实例。
假设您的输入文件就像您问题中的data
字符串。
尝试一下:
#replacements as key-value pairs
replacements = {
'Original View':'New Original View',
'View Source':'New View Source'
}
#Open the input file in read mode and get data as string
with open('your_file.json', 'r') as file:
data = file.read()
#Replace all old value with new values
for old, new in replacements.items():
data = data.replace(old, new)
#Open the output file in write mode and save
with open('output_file.json', 'w') as file:
file.write(data)
输出:
{"menu": {
"header": "SVG Viewer",
"items": [
{"id": "Open"},
{"id": "OpenNew", "label": "Open New"},
null,
{"id": "ZoomIn", "label": "Zoom In"},
{"id": "ZoomOut", "label": "Zoom Out"},
{"id": "OriginalView", "label": "New Original View"},
null,
{"id": "Quality"},
{"id": "Pause"},
{"id": "Mute"},
null,
{"id": "Find", "label": "Find..."},
{"id": "FindAgain", "label": "Find Again"},
{"id": "Copy"},
{"id": "CopyAgain", "label": "Copy Again"},
{"id": "CopySVG", "label": "Copy SVG"},
{"id": "ViewSVG", "label": "View SVG"},
{"id": "ViewSource", "label": "New View Source"},
{"id": "SaveAs", "label": "Save As"},
null,
{"id": "Help"},
{"id": "About", "label": "About Adobe CVG Viewer..."}
]
}}