I have to be able to read a text file and count the number of times the words in a line occur. Plus I have to be able to sort the words from most to least occurring. My code so far is below and I keep getting this error:
Traceback (most recent call last):
File "/Users/lritter/Documents/wordcount.py", line 9, in <module>
lines = file.readlines()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 7927: invalid start byte
Code:
import os
count = {}
os.chdir('/Users/lritter/Desktop/Python')
item = int(input('Which line would you like to evaluate? '))
print('You entered: ', item)
with open('Obama_speech.txt') as file:
lines = file.readlines()
message = (lines[(item)])
message2 = message.split
for word in message2():
if len(word) >= 5:
count[word] = count.get(word,0)+1
print(count)