如何遍历 json 文件并根据特定条件更改特定值
How to loop through a json file and change specific values based on specific criterias
我正在尝试制作一个 python 脚本,在该脚本中我可以根据文件内的特定位置更改特定值,对于此示例,具体为 json 文件。
json 文件长约 100k,"name": "Box #14", "name": "Box #16", "name": "Box #17"
指定了多个区域,列表还在继续。对于每个名称,名称下方都有一个图像字段,例如"image": ".png"
,我想将 .png
值编辑为基于名称编号的特定值。例如,如果 "name": "Box #14
那么 "image": "13.png"
如果 "name": "Box #15 then
"image": "14.png"
等等...
到目前为止我得到的是:
import re
import sys
i = 0
++i
PAT = re.compile('"image": ".png"')
KEYWORDS_PATH = 'images.json'
KEYWORDS = open(KEYWORDS_PATH).read().splitlines()
names = ['"name": ".*"']
def check_all(check, ws):
return all(re.search(r'\b{}\b'.format(w), check) for w in ws)
with open('images.json') as inp, open('output.json', 'w') as out:
for name in names:
if names in KEYWORDS:
print('Removed the keyword - %s' % names)
sys.exit()
for line in inp:
out.write(PAT.sub('"image": "%s.png"' % i, line))
这就是一切0.png
更新:
这是 json 文件中的一个例子
[
{
"name": "Box #14",
"image": ".png",
"attributes": [
{
"trait_type": "Size",
"value": "0.8 inch"
}
]
"files": [
{
"url": ".png",
"type": "image/png"
}
]
}
},
{
"name": "Box #15",
"image": ".png",
"attributes": [
{
"trait_type": "Size",
"value": "2.8 inch"
}
]
"files": [
{
"url": ".png",
"type": "image/png"
}
]
}
}
]
我想要做的就是将图像字段中的 .png 替换为名称上的任何数字,但下方有一个数字,例如如上图 Box #14 name 我想把图片从 .png 替换成 13.png
您可以使用内置的 json
模块来处理 json。这是一个完整的例子。
import json
# read the json file
json_txt = """[ { "name": "Box #14", "image": ".png", "attributes": [ { } ], "files": [ { } ] }, { "name": "Box #15", "image": ".png", "attributes": [ { } ], "files": [ { } ] } ]"""
data = json.loads(json_txt)
for val in data:
number = int(val['name'].split('#')[1])
val['image'] = f"{number-1}.png"
with open('json_output.json', 'w') as outfile:
json.dump(data, outfile)
这假设每个部分都有一个名称和一个图像属性。
对于实际数据,您将像这样读取文件:
with open('data.json') as f:
data = json.load(f)
我正在尝试制作一个 python 脚本,在该脚本中我可以根据文件内的特定位置更改特定值,对于此示例,具体为 json 文件。
json 文件长约 100k,"name": "Box #14", "name": "Box #16", "name": "Box #17"
指定了多个区域,列表还在继续。对于每个名称,名称下方都有一个图像字段,例如"image": ".png"
,我想将 .png
值编辑为基于名称编号的特定值。例如,如果 "name": "Box #14
那么 "image": "13.png"
如果 "name": "Box #15 then
"image": "14.png"
等等...
到目前为止我得到的是:
import re
import sys
i = 0
++i
PAT = re.compile('"image": ".png"')
KEYWORDS_PATH = 'images.json'
KEYWORDS = open(KEYWORDS_PATH).read().splitlines()
names = ['"name": ".*"']
def check_all(check, ws):
return all(re.search(r'\b{}\b'.format(w), check) for w in ws)
with open('images.json') as inp, open('output.json', 'w') as out:
for name in names:
if names in KEYWORDS:
print('Removed the keyword - %s' % names)
sys.exit()
for line in inp:
out.write(PAT.sub('"image": "%s.png"' % i, line))
这就是一切0.png
更新:
这是 json 文件中的一个例子
[
{
"name": "Box #14",
"image": ".png",
"attributes": [
{
"trait_type": "Size",
"value": "0.8 inch"
}
]
"files": [
{
"url": ".png",
"type": "image/png"
}
]
}
},
{
"name": "Box #15",
"image": ".png",
"attributes": [
{
"trait_type": "Size",
"value": "2.8 inch"
}
]
"files": [
{
"url": ".png",
"type": "image/png"
}
]
}
}
]
我想要做的就是将图像字段中的 .png 替换为名称上的任何数字,但下方有一个数字,例如如上图 Box #14 name 我想把图片从 .png 替换成 13.png
您可以使用内置的 json
模块来处理 json。这是一个完整的例子。
import json
# read the json file
json_txt = """[ { "name": "Box #14", "image": ".png", "attributes": [ { } ], "files": [ { } ] }, { "name": "Box #15", "image": ".png", "attributes": [ { } ], "files": [ { } ] } ]"""
data = json.loads(json_txt)
for val in data:
number = int(val['name'].split('#')[1])
val['image'] = f"{number-1}.png"
with open('json_output.json', 'w') as outfile:
json.dump(data, outfile)
这假设每个部分都有一个名称和一个图像属性。
对于实际数据,您将像这样读取文件:
with open('data.json') as f:
data = json.load(f)