python - 追加返回的非元组
问题描述
我正在运行一个脚本以将 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/
解决方案
推荐阅读
- php - 当它们都具有相同的类名时如何选择特定文本字段的值
- html - 尝试使用 css-grid 但并非所有元素似乎都在正确的位置
- ios - 如何在观看之前准备内容?
- html - Firefox/Chrome 兼容性:具有固定标题和列的 HTML 表格
- python - 资源获取是初始化,在 Python 中
- semaphore - 为什么我们需要单个 cpu 上的信号量?
- javascript - 如何从 JavaScript 在 CSV 中动态显示对象?
- python-3.x - 作为消息的列表被转换为字符串
- tensorflow - 如何用我自己的图像构成 FSNS 数据集以用于注意力 OCR tensorflow 模型
- python - Apache Beam Python 字数统计示例因 BeamIOError 导致 Flink Runner 失败