Reputation: 3
I wanna make word count and list how many times word counted. But
f = open("Les.Miserable.txt", 'r')
words = f.read().split()
words.sort()
wordCount = ()
for i in range(len(words)):
words[i] = words[i].replace(".", ""), (",", ""), ("/", ""), ("?", ""), ("!", "")
words[i] = words[i].upper()
if words[i] not in wordCount:
wordCount[words[i]] = 1
else:
wordCount[words[i]] += 1
i can see error message 'tuple' object has no attribute 'upper' in
words[i] = words[i].upper()
here
and also error message 'tuple' object does not support item assignment in
wordCountint[words[i]] = 1
Please let me know know what is the problem
Upvotes: 0
Views: 6274
Reputation: 4037
words[i] = words[i].replace(".", ""), (",", ""), ("/", ""), ("?", ""), ("!", "")
The above code is making words
a tuple. And you cannot perform operations like upper()
on the tuple. Hence, the error.
I think, what you want to do is
words[i] = words[i].replace(".", "").replace(",", "").replace("/", "").replace("?", "").replace("!", "")
Also, you have declare wordCount = ()
as tuple. You won't be able to edit wordCount
because of this, and this will lead to an error. It should be a dictionary : wordCount = {}
The entire program should look like:
f = open("Les.Miserable.txt", 'r')
words = f.read().split()
words.sort()
wordCount = {}
for i in range(len(words)):
words[i] = words[i].replace(".", "").replace(",", "").replace("/", "").replace("?", "").replace("!", "")
words[i] = words[i].upper()
if words[i] not in wordCount:
wordCount[words[i]] = 1
else:
wordCount[words[i]] += 1
sorted_wordCount = sorted(wordCount.items(), key=operator.itemgetter(1), reverse=True)
print sorted_wordCount
Upvotes: 0
Reputation: 4951
in this line:
words[i] = words[i].replace(".", ""), (",", ""), ("/", ""), ("?", ""), ("!", "")
you assign tuple into words[i]
.
i guess you want to replace several character and that you mean to do this:
words[i] = words[i].replace(".", "").replace(",", "").replace("/", "").replace("?", "").replace("!", "")
several values with comma between them are tuple.
1,5,6
is the same as (1,5,6)
so
words[i].replace(".", ""), (",", ""), ("/", ""), ("?", ""), ("!", "")
is the same as
(words[i].replace(".", ""), (",", ""), ("/", ""), ("?", ""), ("!", ""))
in addition, you can't assign into tuple, therefore. the line
wordCount[words[i]] = 1
can throw an exception, you need to change wordCountint
to a dict (when you create it):
wordCount = {}
Upvotes: 0
Reputation: 87084
If you print the value of words[i]
after your attempted character replacements you will see that it is set to a tuple
, e.g.
('word', (',', ''), ('/', ''), ('?', ''), ('!', ''))
So the line that tries to remove unwanted punctuation actually creates a tuple because that's what the comma separated items are, i.e.
words[i].replace(".", ""), (",", ""), ("/", ""), ("?", ""), ("!", "")
is actually a tuple consisting of words[i].replace(".", "")
followed by (",", "")
, etc.
You might have meant to chain a whole lot of replace operations together, but that would need to look like this:
words[i].replace(".", "").replace(",", "").replace("/", "").replace("?", "").replace("!", "")
But that is pretty ugly, and it's restricted to just a few punctuation symbols. str.translate()
is better:
words[i] = words[i].translate(None, '.,/?!')
or, if you want to get rid of all punctuation you can use string.punctuation
:
import string
words[i] = words[i].translate(None, string.punctuation)
Or, if you are using Python 3:
import string
words[i] = words[i].({ord(c):None for c in string.punctuation})
There are other problems in your code, but see if you can correct this first issue first.
Upvotes: 1