简体   繁体   中英

Error: a bytes-like object is required, not 'str'

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)))  

Just cast line into str and error will be gone

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

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM