错误:需要类似字节的对象,而不是' str'

时间:2018-01-22 04:36:25

标签: python

我正在玩我在网上发现的一些代码。它在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)))  

1 个答案:

答案 0 :(得分:2)

只需将line投射到str,错误就会消失

line = line.replace('\r', ' ').replace('\n', ' ')

 line = str(line).replace('\r', ' ').replace('\n', ' ')