简体   繁体   English

为什么当我打开一个文本文件时会这样说,“参数应该是整数或无,而不是‘str’”

[英]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.

相关问题 类型错误:参数应该是整数或无,而不是在 tkinter pyhton 中登录时出现“str”错误 - TypeError: argument should be integer or None, not 'str' error while making sign in tkinter pyhton Python-为什么即使我输入了应该可以使用的密码,它也会说密码被拒绝? - Python- Why does it say password denied even when I enter a password that should work? 当我将 displacy.render(doc, style="dep") 的输出保存到 svg 文件时,有一个 TypeError: write() 参数必须是 str,而不是 None - When I save the output of displacy.render(doc, style="dep") to a svg file, there is a TypeError: write() argument must be str, not None Python:为什么它说我的文件未打开才能读取? - Python: Why does it say that my file is not open to read it? 在网页上搜索字符串时,获取“ TypeError:参数应为整数或类似字节的对象,而不是'str'” - Getting “TypeError: argument should be integer or bytes-like object, not 'str'” when searching for string on web page 在 Python 中,我们应该在参数列表中设置 Optional[str] = None 吗? - In Python should we set Optional[str] = None in the argument list? 为什么在使用Tkinter时Python会说未定义“ App”? - Why does Python say that 'App' is not defined when I use Tkinter? httplib2:- 参数应该是 integer 或类似字节的 object,而不是“str” - httplib2 :- argument should be integer or bytes-like object, not 'str' TypeError:参数应该是 integer 或 None,而不是 '_io.TextIOWrapper' - TypeError: argument should be integer or None, not '_io.TextIOWrapper' psycopg2错误说我在插入文本/字符串时正在插入整数 - psycopg2 error say i am inserting integer when i am inserting text/ string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM