当我存储在note中的文本文件只是一个字符串时,为什么我的代码会说argument should be integer or None, not 'str'
。要打开整数便笺,我应该做什么
f = open("testpoem.txt")
# e.g., f = open("data2.txt")
text = f.read("testpoem.txt")
# text is a string that contains the contents of the entire file
# problem 1
def string2list(text):
return text.split()
if __name__ == '__main__':
print(string2list(text))
下面是全部错误
Traceback (most recent call last): File "C:\Users\filetest.py", line 3, in text = f.read("testpoem.txt") TypeError: argument should be integer or None, not 'str'"
因为read只接受一个参数(size),该参数是“可选的。返回的字节数。默认值为-1,表示整个文件。”
因此,您的代码应该是:
相关问题 更多 >
编程相关推荐