如何删除 python 中的特定 json 值?

How do i remove a specific json value in python?

我正在制作一个可以创建显示和删除播放列表和歌曲的播放列表网站。我正在使用 JSON 文件来存储数据。唯一不起作用的功能是删除歌曲功能。

这是我的 JSON 文件

"Playlists": [
    {
        "ID": 1,
        "Name": "Car Boosted",
        "Songs": [
            {
                "Name": "Dusk Till Dawn",
                "Artist": "Sia",
                "Link": "https://www.youtube.com/watch?v=6ADdqsvlqtU"
            },
            {
                "Name": "Blinding Lights",
                "Artist": "The Weekend",
                "Link": "https://www.youtube.com/watch?v=4NRXx6U8ABQ"
            }
        ],
        "Date": "16 / 03 / 2022"
    },
    {
        "ID": 2,
        "Name": "Workout Playlists",
        "Songs": [
            {
                "Name": "Dusk Till Dawn",
                "Artist": "Sia",
                "Link": "https://www.youtube.com/watch?v=6ADdqsvlqtU"
            },
            {
                "Name": "Blinding Lights",
                "Artist": "The Weekend",
                "Link": "https://www.youtube.com/watch?v=4NRXx6U8ABQ"
            },
            {
                "Name": "Till I Collapes",
                "Artist": "Eminem",
                "Link": "https://www.youtube.com/watch?v=Obim8BYGnOE"
            },
            {
                "Name": "Lose Yourself",
                "Artist": "Eminem",
                "Link": "https://www.youtube.com/watch?v=_Yhyp-_hX2s"
            }
        ],
        "Date": "25 / 04 / 2022"
    }
]

我正在使用播放列表的 ID 和歌曲的索引在列表中找到它并使用它来删除它。 这是我的“删除歌曲”功能:

def deleteSongFromJson(songID, pid):
"""deletes Songs Form Playlist

Args:
        songID (int): ID Of Song
        id (itn): ID Of Playlist
"""
pid = int(pid)
songID = int(songID)
with open('databse.json','r+') as file:
    data = json.load(file)
    playlists = data['Playlists']
    for playlist in playlists:
     if playlist['ID'] == id:
         songs = playlist["Songs"]
         del songs[songID]
         file.seek(0)
         json.dump(data, file, indent = 4)

         

当我 运行 函数时,它会从正确的播放列表中删除歌曲,但会粘贴列表的最后一位并粘贴到 JSON 文件的底部。

例如

                "Name": "Blinding Lights",
                "Artist": "The Weekend",
                "Link": "https://www.youtube.com/watch?v=4NRXx6U8ABQ"
            }
        ],
        "Date": "17 / 05 / 2022"
    }
]
    "Artist": "The Weekend",
                "Link": "https://www.youtube.com/watch?v=4NRXx6U8ABQ"
            }
        ],
        "Date": "17 / 05 / 2022"
    }
]

我认为您的问题是尝试一次性读取和写入文件,这可能会导致奇怪的光标问题。

尝试重构您的代码,使其具有两个单独的 open 语句,如下所示(未经测试):

def deleteSongFromJson(songID, pid):
    """deletes Songs from Playlist

    Args:
        songID (int): ID Of Song
        id (itn): ID Of Playlist
    """
    pid = int(pid)
    songID = int(songID)

    with open('databse.json','r') as file:
        data = json.load(file)

    for playlist in data['Playlists']:
        if playlist['ID'] == id:
            del playlist["Songs"][songID]

    with open('databse.json','w') as file:
        json.dump(data, file, indent=4)