[英]Why does it say this when I open a text file, "argument should be integer or None, not 'str'"
why does my code say argument should be integer or None, not 'str'
when the text file I store in note is just a string.当我存储在 note 中的文本文件只是一个字符串时
argument should be integer or None, not 'str'
为什么我的代码说argument should be integer or None, not 'str'
。 What should I do to open an integer note?.我应该怎么做才能打开整数笔记?。
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))
Here is the full error这是完整的错误
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'"
回溯(最近一次调用):文件“C:\\Users\\filetest.py”,第 3 行,文本 = f.read("testpoem.txt") 类型错误:参数应该是整数或无,而不是“str”
Because read accepte only one argument (size) which "Optional. The number of bytes to return. Default -1, which means the whole file."因为 read 只接受一个参数(大小),其中“可选。返回的字节数。默认 -1,这意味着整个文件。”
So your code should be:所以你的代码应该是:
f = open("testpoem.txt", 'r' )
# e.g., f = open("data2.txt")
text = f.read()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.