回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我正试着把一个字符串变成一个单独的单词列表——除了字母什么都没有。然而,据我所知,unicode是造成这些问题的原因。</p>
<pre><code>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
</code></pre>
<p>我得到这个错误:</p>
<pre><code>AttributeError: 'unicode' object has no attribute 'remove'
</code></pre>
<p>我该怎么解决?</p>
<p>更新:
我不明白为什么我的字符串是unicode的。如果相关,则此模型所属的类如下:</p>
<pre><code>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
</code></pre>
<p>为什么我的字符串是unicode格式的?</p>