UnicodeDecodeError
def getWordFreqs(textPath, stopWordsPath):
wordFreqs = dict()
#open the file in read mode and open stop words
file = open(textPath, 'r')
stopWords = set(line.strip() for line in open(stopWordsPath))
#read the text
text = file.read()
#exclude punctuation and convert to lower case; exclude numbers as well
punctuation = set('!"#$%&\()*+,-./:;<=>?@[\\]^_`{|}~')
text = ''.join(ch.lower() for ch in text if ch not in punctuation)
text = ''.join(ch for ch in text if not ch.isdigit())
#read through the words and add to frequency dictionary
#if it is not a stop word
for word in text.split():
if word not in stopWords:
if word in wordFreqs:
wordFreqs[word] += 1
else:
wordFreqs[word] = 1
I get the following error everytime i try and run this function in python 3.5.2 but it works fine in 3.4.3, I cannot figure out what is causing this error.
line 9, in getWordFreqs
text = file.read()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in position 520: ordinal not in range(128)