[英]AttributeError: 'list' object has no attribute 'text'
I am working with Vader from the nltk package. 我正在使用nltk软件包中的Vader。 I've imported my dataset following the vader tutorial:
我已经按照vader教程导入了我的数据集:
list = []
for line in open("C:\Users\Luca\Desktop\Uni\Tesi\PythonTest\paolo.txt","r").readlines():
for value in line.split(","):
list.append(value)
Then I've created the function to remove punctuation: 然后,我创建了删除标点符号的函数:
def _words_only(self):
text_mod = REGEX_REMOVE_PUNCTUATION.sub('', self.text)
words_only = text_mod.split()
words_only = [word for word in words_only if len(word) > 1]
return words_only
But when I try to use the "words only" function I get this error 但是当我尝试使用“仅单词”功能时,出现此错误
AttributeError Traceback (most recent call last)
<ipython-input-14-cbc12179c890> in <module>()
----> 1 _words_only(list)
<ipython-input-13-68a545bbbaa4> in _words_only(self)
1 def _words_only(self):
----> 2 text_mod = REGEX_REMOVE_PUNCTUATION.sub('', self.text)
3 words_only = text_mod.split()
AttributeError: 'list' object has no attribute 'text'
I am really new to Python. 我真的是Python新手。 Is it a problem in the importing process or is it something else?
是在导入过程中出现问题还是其他问题? Thanks for your help.
谢谢你的帮助。
You don't show where/how you created the function _words_only()
, but the self
argument indicates that you patterned it on a class method. 您没有显示在何处/如何创建函数
_words_only()
,但是self
参数表明您在类方法上对其进行了模式化。 You're evidently using it as a stand-alone function, like this: 您显然正在将其用作独立功能,如下所示:
_words_only(list)
I would advise you not to tackle classes yet if you can avoid it. 如果可以避免的话,我建议您不要上课。 Write your function like this:
编写这样的函数:
def words_only(text):
text_mod = REGEX_REMOVE_PUNCTUATION.sub('', text)
words_only = text_mod.split()
words_only = [word for word in words_only if len(word) > 1]
return words_only
You should also know that your function is designed to process one string, not a list of them. 您还应该知道您的函数旨在处理一个字符串,而不是它们的列表。 In addition, don't use builtin names like
list
as variable names-- you're asking for a very confusing error in a day or two. 此外,请勿将
list
等内置名称用作变量名称,否则您会在一两天内询问一个非常混乱的错误。 Use a more informative name, or an abbreviation like lst
: 使用更具参考价值的名称,或类似
lst
的缩写:
lines = []
...
some_words = words_only(lines[0])
Since you actually want to work with the list of lines, apply the revised function to each one like this: 由于您实际上要使用行列表,因此将修改后的函数应用于每个行,如下所示:
filtered_lines = [ words_only(line) for line in lines ]
If you had wanted to work with the entire contents of the file, you would read in your text like this: 如果您想使用文件的全部内容,则可以这样输入文本:
myfile = open(r"C:\Users\Luca\Desktop\Uni\Tesi\PythonTest\paolo.txt","r")
text = myfile.read()
myfile.close()
some_words = words_only(text)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.