首页 > 解决方案 > 追加返回的非元组

问题描述

我正在运行一个脚本以将 Spotify 专辑 ID(一次 20 个)发送到 Spotify API,并将每个专辑 ID 中的曲目计数保存到一个列表中,然后我将其插入到现有数据框中,然后再保存到 .csv 文件中。

该文件相当大(超过 100 万行),其中大约 900,000 行,我没有遇到任何问题。但是,我最终遇到了 TypeError: "NoneType" object is not subscriptable

常见的问题似乎是 append 函数使用不正确,但据我在这里可以理解,我以正确的方式使用它。我也不知道为什么该函数在崩溃之前运行了 45000 多次迭代。

我的代码是:

length = len(series)
counter = length // 20
remainder = length % 20

list = [''] * 20
finalList = [''] * remainder

i = 0
x = 0
y = 0

trackListCount = 0

output = []

while i < counter:

    j = 0

    while j < 20:
        list[j] = series[x]
        j += 1
        x += 1

    album1 = sp.albums(list)

    j = 0

    while j < 20:
        output.append(album1["albums"][j]["total_tracks"])
        print(album1["albums"][j]["name"])
        j += 1

    i += 1
    print(str(i) + " out of " + str(counter + 1))

if remainder != 0:
    while y < remainder:
        finalList[y] = series[x]
        x += 1
        y += 1

        album2 = sp.albums(finalList)
        y = 0

        while y < remainder:
            output.append(album2["albums"][y]["total_tracks"])
            y += 1

问题出现在第二个 while 循环的行上:

output.append(album1["albums"][j]["total_tracks"])

正如我所说,对于 90% 的文件,脚本运行良好,输出仍然是一个列表,直到突然它没有

任何帮助,将不胜感激

编辑:我忘了提到我在这里使用 Spotipy 库来处理我的请求:https ://spotipy.readthedocs.io/en/2.18.0/

标签: pythonappendtypeerrornonetype

解决方案


推荐阅读