gpt4 book ai didi

python - 类型错误 : "NoneType" object is unsubscriptable

转载 作者:太空宇宙 更新时间:2023-11-03 18:52:25 26 4
gpt4 key购买 nike

使用 Python 运行此代码时出现此错误:

TypeError: "NoneType" object is unsubscriptable".

代码:

number = 0

with open('playlist.txt') as read_number_lines:
for line in read_number_lines:
if line.strip():
number += 1

number = number - 1
print 'number: ', number

for i in range(number):
author_ = raw_input('author: ')
line = input('line: ')
file = open('playlist.txt','a').writelines(' - ' + author_)[line]

如何修复它?

最佳答案

您遇到了一些问题

file = open('playlist.txt','a').writelines(' - ' + author_)[line]

错误的直接根源是 .writelines() 不返回任何内容(因此它返回 None),您尝试使用 对其进行索引>[行]。这会产生你的错误。

此外,您不应该直接在 open() 调用中调用该方法。

整个第二个 for 循环对我来说很神秘。您在循环的每次迭代期间再次打开该文件(您不想这样做;它可能根本不起作用)。

也许你想做类似的事情

with open('playlist.txt','a') as file:
for i in range(number):
author_ = raw_input('author: ')
line = raw_input('line: ')
file.write(author + " - " + line)

但还是很难看出这有什么意义......

关于python - 类型错误 : "NoneType" object is unsubscriptable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18036714/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com