簡體   English   中英

如何使用每個 JSON 文件中的名稱值批量重命名 my.json 文件?

[英]How do I bulk rename my .json files using a name value inside each JSON file?

O 有一個包含 1,800 個 json 文件的文件夾。 我需要使用每個 JSON 文件唯一的鍵值對批量重命名所有 1800 個文件。 例如,每個 JSON 都將其存儲在其中,最初命名為“03801f63a9bf54d2c7b30a7d121c6359-asset.json”,

{
"asset": {
    "format": "PNG",
    "id": "03801f63a9bf54d2c7b30a7d121c6359",
    "name": "IMG_2133.PNG",
    "path": "file:/home/kai/JSON2YOLO/training-images/imagess/IMG_2133.PNG",
    "size": {
        "width": 1920,
        "height": 1080
    },
    "state": 2,
    "type": 1
},
"regions": [
    {
        "id": "SLrFhEAR0",
        "type": "RECTANGLE",
        "tags": [
            "X"
        ],
        "boundingBox": {
            "height": 653.9723502304148,
            "width": 645.9720062208398,
            "left": 1071.9751166407466,
            "top": 99.53917050691241
        },
        "points": [
            {
                "x": 1071.9751166407466,
                "y": 99.53917050691241
            },
            {
                "x": 1717.9471228615864,
                "y": 99.53917050691241
            },
            {
                "x": 1717.9471228615864,
                "y": 753.5115207373271
            },
            {
                "x": 1071.9751166407466,
                "y": 753.5115207373271
            }
        ]
    },
    {
        "id": "SEEt8pqnV",
        "type": "RECTANGLE",
        "tags": [
            "Y"
        ],
        "boundingBox": {
            "height": 421.7972350230415,
            "width": 576.049766718507,
            "left": 17.671073094867808,
            "top": 89.13810483870968
        },
        "points": [
            {
                "x": 17.671073094867808,
                "y": 89.13810483870968
            },
            {
                "x": 593.7208398133748,
                "y": 89.13810483870968
            },
            {
                "x": 593.7208398133748,
                "y": 510.9353398617512
            },
            {
                "x": 17.671073094867808,
                "y": 510.9353398617512
            }
        ]
    }
],
"version": "2.2.0" }

基本上需要將文件名從“03801f63a9bf54d2c7b30a7d121c6359-asset.json”重命名為“IMG_2133”,擴展名為“.Z466DEEC76ECDF5FCA6D385731F6324D5421”的.json文件名結束了我需要能夠接收所有 1800 個 json 文件並用它們的名稱值重命名它們。

因此,您應該枚舉目錄中的所有文件。 使用 json 庫打開它們,從 json 中讀取它們的名稱,然后將文件重命名為該文件。

這是我快速整理的內容。 我不保證這會起作用,但它應該給你一個大致的想法。

import os
import json
from os import listdir
from os.path import isfile, join

files = [f for f in listdir(os.getcwd()) if isfile(join(os.getcwd(), f))]

for f in files:
  if f.endswith('.json'):
    openFile=open(f,"r")
    fileData = json.load(openFile)
    oldName = fileData['asset']['name']
    newName = oldName.replace('.PNG', '.json')
    os.rename(f,newName)
    print("Renamed "+ oldName + " to " + newName)

這應該在3個條件下工作:

-您在 windows 上(因為我在路徑中使用了“/”)

-所有新文件名都不同(原因很明顯)

- 並且新文件名不包含點字符(如果某些名稱包含點,請告訴我我可以修改腳本來處理這些情況)

import json                                                         # this module is used to read json files
import os                                                           # this modules is used to get all the names of your json files and rename them

folder_path = "test_folder"                                         # you specifie the folder conatining your json files

files = os.listdir(folder_path)                                     # you all the files names in the specified folder

for file in files:                                                  # for each file in the folder
    with open(f"{folder_path}/{file}", "r") as f:                   # you open the file
        json_file = json.load(f)                                    # you get the infos stored in the json file as a dictionary
    filename = json_file["asset"]["name"].split(".")[0] + ".json"   # you generate the new filename from the dictionary
    os.rename(f"{folder_path}/{file}", f"{folder_path}/{filename}") # you rename the file

您只需將文件所在文件夾的路徑放在 folder_path 變量中(第 4 行)

我希望這可以幫到你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM