如何遍历 Spotipy 的 playlist_items 函数中的嵌套列表并仅打印出我感兴趣的字段?

Posted

技术标签:

【中文标题】如何遍历 Spotipy 的 playlist_items 函数中的嵌套列表并仅打印出我感兴趣的字段?【英文标题】:How do I iterate through a nested list from Spotipy's playlist_items function and only print out the fields I'm interested in? 【发布时间】:2021-12-28 05:00:31 【问题描述】:

我正在尝试使用 Spotipy 从播放列表中获取所有曲目名称和相应的艺术家。我现在可以获取字段的 json 转储,但我无法弄清楚如何遍历转储并打印出我感兴趣的字段。

这是我用来获取有关播放列表的所有信息的内容:

playlist = spotipy.playlist_items(playlist_id)

如果我然后print(json.dumps(playlist, indent=2)),那么我得到以下输出:


  "collaborative": false,
  "description": "",
  "external_urls": 
    "spotify": "https://open.spotify.com/playlist/50uWPcNFdJElMVZWo0IebB"
  ,
  "followers": 
    "href": null,
    "total": 0
  ,
  "href": "https://api.spotify.com/v1/playlists/50uWPcNFdJElMVZWo0IebB?additional_types=track",
  "id": "50uWPcNFdJElMVZWo0IebB",
  "images": [
    
      "height": 640,
      "url": "https://i.scdn.co/image/ab67616d0000b273942a0c9ac8f1def7c8805044",
      "width": 640
    
  ],
  "name": "Spotipy Test",
  "owner": 
    "display_name": "username",
    "external_urls": 
      "spotify": "https://open.spotify.com/user/username"
    ,
    "href": "https://api.spotify.com/v1/users/username",
    "id": "username",
    "type": "user",
    "uri": "spotify:user:username"
  ,
  "primary_color": null,
  "public": false,
  "snapshot_id": "Nyw0OTk4MDg1NDM3NzRlOWI2MGY2MDc2ZjMxNTE4OGFkZWQ5Y2QyZDdk",
  "tracks": 
    "href": "https://api.spotify.com/v1/playlists/50uWPcNFdJElMVZWo0IebB/tracks?offset=0&limit=100&additional_types=track",
    "items": [
      
        "added_at": "2021-11-17T07:32:59Z",
        "added_by": 
          "external_urls": 
            "spotify": "https://open.spotify.com/user/username"
          ,
          "href": "https://api.spotify.com/v1/users/username",
          "id": "username",
          "type": "user",
          "uri": "spotify:user:username"
        ,
        "is_local": false,
        "primary_color": null,
        "track": 
          "album": 
            "album_type": "album",
            "artists": [
              
                "external_urls": 
                  "spotify": "https://open.spotify.com/artist/6fOMl44jA4Sp5b9PpYCkzz"
                ,
                "href": "https://api.spotify.com/v1/artists/6fOMl44jA4Sp5b9PpYCkzz",
                "id": "6fOMl44jA4Sp5b9PpYCkzz",
                "name": "NF",
                "type": "artist",
                "uri": "spotify:artist:6fOMl44jA4Sp5b9PpYCkzz"
              
            ],
            "available_markets": [],
            "external_urls": 
              "spotify": "https://open.spotify.com/album/46xdC4Qcvscfs3Ai2RIHcv"
            ,
            "href": "https://api.spotify.com/v1/albums/46xdC4Qcvscfs3Ai2RIHcv",
            "id": "46xdC4Qcvscfs3Ai2RIHcv",
            "images": [
              
                "height": 640,
                "url": "https://i.scdn.co/image/ab67616d0000b273942a0c9ac8f1def7c8805044",
                "width": 640
              ,
              
                "height": 300,
                "url": "https://i.scdn.co/image/ab67616d00001e02942a0c9ac8f1def7c8805044",
                "width": 300
              ,
              
                "height": 64,
                "url": "https://i.scdn.co/image/ab67616d00004851942a0c9ac8f1def7c8805044",
                "width": 64
              
            ],
            "name": "The Search",
            "release_date": "2019-07-26",
            "release_date_precision": "day",
            "total_tracks": 20,
            "type": "album",
            "uri": "spotify:album:46xdC4Qcvscfs3Ai2RIHcv"
          ,
          "artists": [
            
              "external_urls": 
                "spotify": "https://open.spotify.com/artist/6fOMl44jA4Sp5b9PpYCkzz"
              ,
              "href": "https://api.spotify.com/v1/artists/6fOMl44jA4Sp5b9PpYCkzz",
              "id": "6fOMl44jA4Sp5b9PpYCkzz",
              "name": "NF",
              "type": "artist",
              "uri": "spotify:artist:6fOMl44jA4Sp5b9PpYCkzz"
            
          ],
          "available_markets": [],
          "disc_number": 1,
          "duration_ms": 248053,
          "episode": false,
          "explicit": false,
          "external_ids": 
            "isrc": "USUM71907048"
          ,
          "external_urls": 
            "spotify": "https://open.spotify.com/track/3oLe5ZILASG8vU5dxIMfLY"
          ,
          "href": "https://api.spotify.com/v1/tracks/3oLe5ZILASG8vU5dxIMfLY",
          "id": "3oLe5ZILASG8vU5dxIMfLY",
          "is_local": false,
          "name": "The Search",
          "popularity": 74,
          "preview_url": "https://p.scdn.co/mp3-preview/c26b3dedc967f5009451a693ac275e50c6ecb53c?cid=1f51bd6bdbd94e34884f3ec17c0f68ed",
          "track": true,
          "track_number": 1,
          "type": "track",
          "uri": "spotify:track:3oLe5ZILASG8vU5dxIMfLY"
        ,
        "video_thumbnail": 
          "url": null
        
      ,
      
        "added_at": "2021-11-17T07:33:24Z",
        "added_by": 
          "external_urls": 
            "spotify": "https://open.spotify.com/user/username"
          ,
          "href": "https://api.spotify.com/v1/users/username",
          "id": "username",
          "type": "user",
          "uri": "spotify:user:username"
        ,
        "is_local": false,
        "primary_color": null,
        "track": 
          "album": 
            "album_type": "album",
            "artists": [
              
                "external_urls": 
                  "spotify": "https://open.spotify.com/artist/3uL4UpqShC4p2x1dJutoRW"
                ,
                "href": "https://api.spotify.com/v1/artists/3uL4UpqShC4p2x1dJutoRW",
                "id": "3uL4UpqShC4p2x1dJutoRW",
                "name": "Ghostemane",
                "type": "artist",
                "uri": "spotify:artist:3uL4UpqShC4p2x1dJutoRW"
              
            ],
            "available_markets": [],
            "external_urls": 
              "spotify": "https://open.spotify.com/album/4pzCKSWYNM2yUWrNvycC6e"
            ,
            "href": "https://api.spotify.com/v1/albums/4pzCKSWYNM2yUWrNvycC6e",
            "id": "4pzCKSWYNM2yUWrNvycC6e",
            "images": [
              
                "height": 640,
                "url": "https://i.scdn.co/image/ab67616d0000b27347fa4895a04fbb517fd42070",
                "width": 640
              ,
              
                "height": 300,
                "url": "https://i.scdn.co/image/ab67616d00001e0247fa4895a04fbb517fd42070",
                "width": 300
              ,
              
                "height": 64,
                "url": "https://i.scdn.co/image/ab67616d0000485147fa4895a04fbb517fd42070",
                "width": 64
              
            ],
            "name": "Hexada",
            "release_date": "2017-09-05",
            "release_date_precision": "day",
            "total_tracks": 10,
            "type": "album",
            "uri": "spotify:album:4pzCKSWYNM2yUWrNvycC6e"
          ,
          "artists": [
            
              "external_urls": 
                "spotify": "https://open.spotify.com/artist/3uL4UpqShC4p2x1dJutoRW"
              ,
              "href": "https://api.spotify.com/v1/artists/3uL4UpqShC4p2x1dJutoRW",
              "id": "3uL4UpqShC4p2x1dJutoRW",
              "name": "Ghostemane",
              "type": "artist",
              "uri": "spotify:artist:3uL4UpqShC4p2x1dJutoRW"
            
          ],
          "available_markets": [],
          "disc_number": 1,
          "duration_ms": 124538,
          "episode": false,
          "explicit": true,
          "external_ids": 
            "isrc": "TCADG1741539"
          ,
          "external_urls": 
            "spotify": "https://open.spotify.com/track/2Vc6NJ9PW9gD9q343XFRKx"
          ,
          "href": "https://api.spotify.com/v1/tracks/2Vc6NJ9PW9gD9q343XFRKx",
          "id": "2Vc6NJ9PW9gD9q343XFRKx",
          "is_local": false,
          "name": "Mercury: Retrograde",
          "popularity": 76,
          "preview_url": "https://p.scdn.co/mp3-preview/7d4cb282d38d48d85e9faedb3ea5e74546281d07?cid=1f51bd6bdbd94e34884f3ec17c0f68ed",
          "track": true,
          "track_number": 4,
          "type": "track",
          "uri": "spotify:track:2Vc6NJ9PW9gD9q343XFRKx"
        ,
        "video_thumbnail": 
          "url": null
        
      
    ],
    "limit": 100,
    "next": null,
    "offset": 0,
    "previous": null,
    "total": 2
  ,
  "type": "playlist",
  "uri": "spotify:playlist:50uWPcNFdJElMVZWo0IebB"

这仅适用于播放列表中的两首歌曲!

在这一点上,我对 python 的有限知识让我失望了,因为我不知道如何从这里开始。我要查找的只是曲目名称和相应的艺术家。

那么,我该如何遍历海量的嵌套列表,只打印出我感兴趣的字段呢?

【问题讨论】:

查看 Pyhon 文档,了解如何使用列表/字典:docs.python.org/3/tutorial/datastructures.html 【参考方案1】:

您可以使用 fields 变量来指定您想要的信息。 例如与 playlist = sp.playlist_items(paylist_id, fields='items.track.artists.name,items.track.name'), 您只会得到艺术家姓名和曲目名称信息: "items": ["track": "artists": ["name": "Avicii"], "name": "Levels - Radio Edit", "track": "artists": ["name": "Avicii"], "name": "Wake Me Up - Radio Edit"]

【讨论】:

好的,我现在明白了。我无法弄清楚如何遍历嵌套列表

以上是关于如何遍历 Spotipy 的 playlist_items 函数中的嵌套列表并仅打印出我感兴趣的字段?的主要内容,如果未能解决你的问题,请参考以下文章

如何解决:使用 Spotipy 的 Python 中客户端范围不足

spotipy授权代码流程

Spotipy - 从公共播放列表访问曲目,无需身份验证

使用 Spotipy 设置 Spotify 凭据

将多个范围与 spotipy 一起使用

Spotipy 使用授权代码流刷新令牌