Reputation: 159
I was playing around with some code i found online. It's in Python 2. When i ran the code in Python 3, it gives me this error: a byte-like object is required, not 'str'. can someone help me fix this? thank you very much
import urllib.request as ur
text =
ur.urlopen('https://raw.githubusercontent.com/ryanmcdermott/trump-
speeches/master/speeches.txt')
words = []
for line in text:
line = line.decode('utf-8-sig', errors='ignore')
line = line.encode('ascii', errors='ignore')
line = line.replace('\r', ' ').replace('\n', ' ')
new_words = line.split(' ')
new_words = [word for word in new_words if word not in ['', ' ']]
words = words + new_words
print('Corpus size: {0} words.'.format(len(words)))
Upvotes: 2
Views: 11325
Reputation: 1433
Just cast line
into str
and error will be gone
line = line.replace('\r', ' ').replace('\n', ' ')
to
line = str(line).replace('\r', ' ').replace('\n', ' ')
Upvotes: 9