我正在玩我在网上发现的一些代码。它在Python 2中。当我在Python 3中运行代码时,它给了我这个错误:需要一个类似字节的对象,而不是' str'。有人可以帮我解决这个问题吗?非常感谢你
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)))
答案 0 :(得分:2)
只需将line
投射到str
,错误就会消失
line = line.replace('\r', ' ').replace('\n', ' ')
到
line = str(line).replace('\r', ' ').replace('\n', ' ')