Reputation: 103
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.
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:
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?
Upvotes: 2
Views: 30180
Reputation: 177901
Do you have a from __future__ import unicode_literals
in your code? That would cause Python 2.X to treat 'string'
as Unicode.
As others have said, strings aren't mutable and do not have a remove
method.
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}
Upvotes: 1
Reputation: 1381
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]
Upvotes: 0
Reputation: 236104
The error is explicit: the n
variable, which is a string, doesn't have a remove
method - that's because strings are immutable in Python. You'll have to create a new string without the characters you want to remove.
Upvotes: 2