如何使用python修改JSON文件中的多个值

如何使用python修改JSON文件中的多个值,python,json,Python,Json,我正在尝试修改JSON文件,其中需要将文件名替换为基本路径 结构如下: data = {"images": [ { "id": 1, "file_name": "Folder1/Folder2/Folder3/Folder4/1110.jpg", "height": 3024, "width": 4032

我正在尝试修改JSON文件,其中需要将文件名替换为基本路径 结构如下:

data = {"images": [
    {
        "id": 1,
        "file_name": "Folder1/Folder2/Folder3/Folder4/1110.jpg",
        "height": 3024,
        "width": 4032
    },
    {
        "id": 2,
        "file_name": "Folder1/Folder2/Folder3/Folder4/1111.jpg",
        "height": 3024,
        "width": 4032
    },
    {
        "id": 3,
        "file_name": "Folder1/Folder2/Folder3/Folder4/1112.jpg",
        "height": 3024,
        "width": 4032
    }
]}
这是我试过的代码

data = json.load(open(".\example.json"))
首先,我用下面的代码检查完整的文件名

for img in data['images']:
    print(img["file_name"])
下面是此单元格的输出

Folder1/Folder2/Folder3/Folder4/1110.jpg
Folder1/Folder2/Folder3/Folder4/1110.jpg
Folder1/Folder2/Folder3/Folder4/1110.jpg
然后我对它进行了修改,以获得如下所示的基本路径

for feature in data['images']:
    feature['file_name'] = os.path.basename(feature["file_name"])
    print(feature['file_name'])
这是输出:

1110.jpg
1111.jpg
1112.jpg
当我试图将其转储到JSON文件中时,我只得到一个文件名

data = {1110.jpg}
我想得到如下输出。请帮我提供您的意见

data = {"images": [
    {
        "id": 1,
        "file_name": "1110.jpg",
        "height": 3024,
        "width": 4032
    },
    {
        "id": 2,
        "file_name": "1111.jpg",
        "height": 3024,
        "width": 4032
    },
    {
        "id": 3,
        "file_name": "1112.jpg",
        "height": 3024,
        "width": 4032
    }
]}

问题在这一行
关于临时['images']中的功能:
,请将其更改为
关于数据['images']中的功能。

数据['images']中的功能的
:
功能['file\u name']=os.path.basename(功能['file\u name'])
打印(数据)
[{'file_name':'1110.jpg','height':3024',id':1',width':4032},
{'file_name':'1111.jpg','height':3024',id':2',width':4032},
{'file_name':'1112.jpg','height':3024',id':3',width':4032}]

问题在这一行
对于临时['images']中的功能:
,请将其更改为
对于数据['images']中的功能。

数据['images']中的功能的
:
功能['file\u name']=os.path.basename(功能['file\u name'])
打印(数据)
[{'file_name':'1110.jpg','height':3024',id':1',width':4032},
{'file_name':'1111.jpg','height':3024',id':2',width':4032},
{'file_name':'1112.jpg','height':3024',id':3',width':4032}]

如果已解决变量名称差异,则代码将按预期工作

此外,我还建议使用上下文管理器
with
打开该文件,以便在退出
with
块时自动关闭该文件

import json, os
with open(".\example.json", 'r') as f:
    data = json.load(f)

for feature in data['images']:
    feature['file_name'] = os.path.basename(feature["file_name"])
    
print(data)
输出:

json
文件中的数据:

{'images': [{'id': 1, 'file_name': 'Folder1/Folder2/Folder3/Folder4/1110.jpg', 'height': 3024, 'width': 4032}, {'id': 2, 'file_name': 'Folder1/Folder2/Folder3/Folder4/1111.jpg', 'height': 3024, 'width': 4032}, {'id': 3, 'file_name': 'Folder1/Folder2/Folder3/Folder4/1112.jpg', 'height': 3024, 'width': 4032}]}
格式化数据:

{'images': [{'id': 1, 'file_name': '1110.jpg', 'height': 3024, 'width': 4032}, {'id': 2, 'file_name': '1111.jpg', 'height': 3024, 'width': 4032}, {'id': 3, 'file_name': '1112.jpg', 'height': 3024, 'width': 4032}]}

如果您已经解决了变量名的差异,那么代码将按预期工作

此外,我还建议使用上下文管理器
with
打开该文件,以便在退出
with
块时自动关闭该文件

import json, os
with open(".\example.json", 'r') as f:
    data = json.load(f)

for feature in data['images']:
    feature['file_name'] = os.path.basename(feature["file_name"])
    
print(data)
输出:

json
文件中的数据:

{'images': [{'id': 1, 'file_name': 'Folder1/Folder2/Folder3/Folder4/1110.jpg', 'height': 3024, 'width': 4032}, {'id': 2, 'file_name': 'Folder1/Folder2/Folder3/Folder4/1111.jpg', 'height': 3024, 'width': 4032}, {'id': 3, 'file_name': 'Folder1/Folder2/Folder3/Folder4/1112.jpg', 'height': 3024, 'width': 4032}]}
格式化数据:

{'images': [{'id': 1, 'file_name': '1110.jpg', 'height': 3024, 'width': 4032}, {'id': 2, 'file_name': '1111.jpg', 'height': 3024, 'width': 4032}, {'id': 3, 'file_name': '1112.jpg', 'height': 3024, 'width': 4032}]}

创建一个列表并将所有输出追加到该列表中,然后遍历该列表,并将其中的项目添加到json文件中,或者直接执行该操作。您还可以提供更多关于如何转储这些json文件的代码吗?谢谢您的快速响应@Matiiss,是的,我错过了转储json文件。不管怎样,下面的答案解决了我的问题。创建一个列表并将所有输出附加到那里,然后遍历列表,并将列表中的项目添加到json文件中,或者直接执行。您还可以提供更多关于如何转储这些json文件的代码吗?谢谢您的快速响应@Matiiss,是的,我错过了转储json文件。不管怎样,下面的答案解决了我的疑问。谢谢你@Krishna,它完全按照我的期望工作。正如您提到的,这就是变量名差异问题。我改正了。很高兴问题很快解决了!谢谢你@Krishna,它完全按照我的期望工作。正如您提到的,这就是变量名差异问题。我改正了。很高兴问题很快解决了!谢谢你Sancith Jain谢谢你Sancith Jain。