我正试图修改我的JSON文件,我需要用基本路径替换file_name。 结构如下。
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
Here is my code which I tried
data = json.load(open(".\example.json"))
首先,我用以下代码检查了完整的文件名
for img in data['images']:
print(img["file_name"])
Below is the Output for this cell
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'])
This is the output:
1110.jpg
1111.jpg
1112.jpg
当我试图将其转储为一个JSON文件时,但我只得到一个file_name。
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