Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我收到此错误';str';对象没有属性';文本';当我尝试运行以下代码时_Python_Nlp_Lexical - Fatal编程技术网

Python 我收到此错误';str';对象没有属性';文本';当我尝试运行以下代码时

Python 我收到此错误';str';对象没有属性';文本';当我尝试运行以下代码时,python,nlp,lexical,Python,Nlp,Lexical,我删除了.text,刚刚编写了peter_pan.split(),但我不确定输出是否正确 导入请求 从nltk导入FreqDist url=”https://www.gutenberg.org/files/16/16-0.txt" peter_pan=requests.get(url.text) peter\u pan\u words=peter\u pan.text.split() 单词频率=频率分布(彼得·潘单词) 频率=单词的频率。最常见(3)[2][1] 打印(频率) 这部分代码: pe

我删除了.text,刚刚编写了peter_pan.split(),但我不确定输出是否正确

导入请求
从nltk导入FreqDist
url=”https://www.gutenberg.org/files/16/16-0.txt"
peter_pan=requests.get(url.text)
peter\u pan\u words=peter\u pan.text.split()
单词频率=频率分布(彼得·潘单词)
频率=单词的频率。最常见(3)[2][1]
打印(频率)

这部分代码:

peter_pan = requests.get(url).text
调用
.text
时,您正在将对象转换为字符串。因此,由于它现在是一个字符串,因此不能再次对其调用
.text
。简单更改:

peter_pan_words = peter_pan.text.split()

你在做什么:

peter_pan = requests.get(url).text
您使用的
文本是
请求返回的响应对象的方法。get
。此方法返回一个字符串(包含页面内容)。这一切都很好

但是,您正在执行以下操作:

peter_pan_words = peter_pan.text.split()
这次您尝试访问
str
对象的
text
方法,但是
str
没有任何名为
text
的方法。如果执行打印(dir(peter_pan))
,您将看到可供访问的属性(方法/属性)。您应该看到类似的内容:

['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
您将看到
text
不是其中之一(这就是为什么会出现错误),但您还将看到
split
就在那里——事实上,您只需直接调用
split

peter_pan_words = peter_pan.split()

欢迎您可以通过选择代码并按Ctrl+k来格式化代码。
peter_pan_words = peter_pan.split()