【问题标题】:How do I bulk rename my .json files using a name value inside each JSON file?如何使用每个 JSON 文件中的名称值批量重命名 .json 文件?
【发布时间】:2020-11-23 06:45:17
【问题描述】:

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”,JSON 扩展名为.json,以便文件名最终为“IMG_2133.json”。我需要能够接收所有 1800 个 json 文件并用它们的名称值重命名它们。

【问题讨论】:

    标签: python json rename file-rename


    【解决方案1】:

    因此,您应该枚举目录中的所有文件。使用 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)
    

    【讨论】:

    • 我认为这不会起作用,因为在 json 文件中,有一个包含“PNG”的格式值,所以可能旧文件名并不总是以“.PNG”结尾跨度>
    • 是的,你说得对。但是可以通过将这一行 newName = oldName.replace('.PNG', '.json') 更改为 newName = oldName.replace('.'+fileData['asset']['format'], '.json') 来修复它
    【解决方案2】:

    这应该在 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 行)

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-26
      • 2021-08-21
      • 1970-01-01
      • 2010-10-17
      相关资源
      最近更新 更多