AttributeError:“列表"对象没有属性“文本" [英] AttributeError: 'list' object has no attribute 'text'

查看:359
本文介绍了AttributeError:“列表"对象没有属性“文本"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用nltk软件包中的Vader. 我已按照vader教程导入了数据集:

I am working with Vader from the nltk package. I've imported my dataset following the vader tutorial:

    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'

我真的是Python新手.是在导入过程中出现问题还是其他问题?感谢您的帮助.

I am really new to Python. Is it a problem in the importing process or is it something else? Thanks for your help.

推荐答案

您没有显示函数_words_only()的创建位置/方式,但是self参数表明您在类方法上对其进行了模式化.显然,您正在将其用作独立功能,如下所示:

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. 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

您还应该知道您的函数旨在处理一个字符串,而不是它们的列表.另外,请勿将诸如list之类的内置名称用作变量名称,而是要在一两天内提出一个非常令人困惑的错误.使用更具参考性的名称,或使用lst这样的缩写:

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. Use a more informative name, or an abbreviation like 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)

这篇关于AttributeError:“列表"对象没有属性“文本"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆