Python AttributeError: 'unicode' 对象没有属性 'remove'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19967961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:07:10  来源:igfitidea点击:

AttributeError: 'unicode' object has no attribute 'remove'

pythonunicode

提问by tog

I'm trying to turn a string into a list of separate words--nothing but letters. However, as far as I can tell, unicode is causing the problems.

我试图把一个字符串变成一个单独的单词列表——除了字母之外什么都没有。但是,据我所知,unicode 导致了问题。

essay_text = ['This,', 'this,', 'this', 'and', 'that.']

def create_keywords(self):
    low_text = self.essay_text.lower()
    word_list = low_text.split()
    abcs = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'x', 'y', 'z']
    for n in word_list:
        for m in n:
            for l in abcs:
                if m!=l:
                    n.remove(m)
        self.keywords.setdefault(n, 0)
        self.keywords[n] = word_list.count(n)
        for m in bad_words:
            if n==m:
                del self.keywords[n]
    print self.keywords

I get this error:

我收到此错误:

AttributeError: 'unicode' object has no attribute 'remove'

How can I solve this?

我该如何解决这个问题?

Update: I don't understand why my strings are in unicode. If it is relevant, here is the class that this model lies under:

更新:我不明白为什么我的字符串是 unicode。如果相关,这里是这个模型所在的类:

class Essay(models.Model):
    title = models.CharField(max_length=100)
    author = models.CharField(max_length=100)
    email = models.EmailField(max_length=100)
    essay_text = models.TextField()
    sources = models.TextField()

    def __unicode__(self):
         return self.title

Why are my strings in in unicode?

为什么我的字符串在 unicode 中?

采纳答案by Mark Tolonen

Do you have a from __future__ import unicode_literalsin your code? That would cause Python 2.X to treat 'string'as Unicode.

你有没有from __future__ import unicode_literals在你的代码?这将导致 Python 2.X 被'string'视为 Unicode。

As others have said, strings aren't mutable and do not have a removemethod.

正如其他人所说,字符串不是可变的,也没有remove方法。

There are a couple of modules that greatly simplify your goal:

有几个模块可以极大地简化您的目标:

import re
from collections import Counter

bad_words = ['and']

def create_keywords():
    essay_text = 'This, this, this and that.'
    # This regular expression finds consecutive strings of lowercase letters.
    # Counter counts each unique string and collects them in a dictionary.
    result = Counter(re.findall(r'[a-z]+',essay_text.lower()))
    for w in bad_words:
        result.pop(w)
    return dict(result) # return a plain dict instead of a Counter object.

Output:

输出:

>>> create_keywords()
{'this': 3, 'that': 1}

回答by óscar López

The error is explicit: the nvariable, which is a string, doesn't have a removemethod - that's because strings are immutable in Python. You'll have to create a new string without the characters you want to remove.

错误是明确的:n作为字符串的变量没有remove方法 - 那是因为字符串在 Python 中是不可变的。您必须创建一个没有要删除的字符的新字符串。

回答by John Spong

Strings are immutable, meaning they cannot be changed. What you'll really need to do is create a new string in its place with just the letters:

字符串是不可变的,这意味着它们不能被改变。您真正需要做的是在其位置创建一个仅包含字母的新字符串:

def just_letters(s):
    return ''.join(l for l in s if l in string.lowercase)

word_list = [just_letters(word) for word in word_list]