Python - 从 JSON 中删除包含特定值的十一元

问题描述 投票:0回答:1

我正在尝试从特定艺术家的 Spotify 流数据中批量删除一堆项目(主要是试图删除一堆有声读物)。数据采用一堆 JSON 的形式,格式如下:

  {
    "ts": "2017-05-04T13:26:03Z",
    "username": [redacted],
    "platform": "Android OS 5.1.1 API 22 (samsung, SM-J500H)",
    "ms_played": 11887,
    "conn_country": "GB",
    "ip_addr_decrypted": null,
    "user_agent_decrypted": null,
    "master_metadata_track_name": "Just Breathe",
    "master_metadata_album_artist_name": "Pearl Jam",
    "master_metadata_album_album_name": "Backspacer",
    "spotify_track_uri": "spotify:track:2m2dgHoAEZUkmxamsOwyQg",
    "episode_name": null,
    "episode_show_name": null,
    "spotify_episode_uri": null,
    "reason_start": "clickrow",
    "reason_end": null,
    "shuffle": "FALSE",
    "skipped": null,
    "offline": "FALSE",
    "offline_timestamp": 1.49385E+12,
    "incognito_mode": "FALSE"
  },
  {
    "ts": "2017-05-04T13:26:32Z",
    "username": [redacted],
    "platform": "Android OS 5.1.1 API 22 (samsung, SM-J500H)",
    "ms_played": 11887,
    "conn_country": "GB",
    "ip_addr_decrypted": null,
    "user_agent_decrypted": null,
    "master_metadata_track_name": "Just Breathe",
    "master_metadata_album_artist_name": "Pearl Jam",
    "master_metadata_album_album_name": "Backspacer",
    "spotify_track_uri": "spotify:track:2m2dgHoAEZUkmxamsOwyQg",
    "episode_name": null,
    "episode_show_name": null,
    "spotify_episode_uri": null,
    "reason_start": "clickrow",
    "reason_end": null,
    "shuffle": "FALSE",
    "skipped": null,
    "offline": "FALSE",
    "offline_timestamp": 1.49385E+12,
    "incognito_mode": "FALSE"
  },
  {
    "ts": "2017-05-04T13:30:17Z",
    "username": [redacted],
    "platform": "Android OS 5.1.1 API 22 (samsung, SM-J500H)",
    "ms_played": 0,
    "conn_country": "GB",
    "ip_addr_decrypted": null,
    "user_agent_decrypted": null,
    "master_metadata_track_name": "The Sirens of Time Part 1, Track 1",
    "master_metadata_album_artist_name": "Doctor Who",
    "master_metadata_album_album_name": "Main Range 1: The Sirens of Time",
    "spotify_track_uri": "spotify:track:64vQITW6SxB2fgxulsZ5Ix",
    "episode_name": null,
    "episode_show_name": null,
    "spotify_episode_uri": null,
    "reason_start": "clickrow",
    "reason_end": null,
    "shuffle": "FALSE",
    "skipped": null,
    "offline": "FALSE",
    "offline_timestamp": 1.4939E+12,
    "incognito_mode": "FALSE"
  },

例如,我如何删除“master_metadata_album_artist_name”等于“Doctor Who”的所有元素?有没有办法同时对多个 JSON 文件执行此操作?

尝试过:

import json
obj  = json.load(open("Streaming_History_Audio_2016-2018_1.json"))
                                                      
for i in xrange(len(obj)):
    if obj[i]["master_metadata_album_artist_name"] == "Doctor Who":
        obj.pop(i)
        break
                                     
open("updated-file.json", "w").write(
    json.dumps(obj, sort_keys=True, indent=4, separators=(',', ': '))
)

结果:

C:\Users\tim_f\Downloads\my_spotify_data(2)\Spotify Extended Streaming History 2023 - Copy>py del.py
Traceback (most recent call last):
  File "C:\Users\tim_f\Downloads\my_spotify_data(2)\Spotify Extended Streaming History 2023 - Copy\del.py", line 2, in <module>
    obj  = json.load(open("Streaming_History_Audio_2016-2018_1.json"))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\tim_f\AppData\Local\Programs\Python\Python312\Lib\json\__init__.py", line 293, in load
    return loads(fp.read(),
                 ^^^^^^^^^
  File "C:\Users\tim_f\AppData\Local\Programs\Python\Python312\Lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 181341: character maps to <undefined>
python arrays json spotify
1个回答
0
投票

您在打开文件时遇到错误。这很可能是由于编码问题造成的。
试试这个:

obj = json.load(open("Streaming_History_Audio_2016-2018_1.json", encoding="utf8"))
© www.soinside.com 2019 - 2024. All rights reserved.