Python3 Error: TypeError: Can't convert 'bytes' object to str implicitly

天大地大妈咪最大 提交于 2019-11-27 07:04:41
Ashwini Chaudhary

urlopen() returns a bytes object, to perform string operations over it you should convert it to str first.

for word in urlopen(WORD_URL).readlines():
    WORDS.append(word.strip().decode('utf-8')) # utf-8 works in your case

To get the correct charset : How to download any(!) webpage with correct charset in python?

In Python 3, the urlopen function returns an HTTPResponse object, which acts like a binary file. So, when you do this:

for word in urlopen(WORD_URL).readlines():
    WORDS.append(word.strip())

… you end up with a bunch of bytes objects instead of str objects. So when you do this:

result = result.replace("###", word, 1)

… you end up trying to replace the string "###" within the string result with a bytes object, instead of a str. Hence the error:

TypeError: Can't convert 'bytes' object to str implicitly

The answer is to explicitly decode the words as soon as you get them. To do that, you have to figure out the right encoding from the HTTP headers. How do you do that?

In this case, I read the headers, I can tell that it's ASCII, and it's obviously a static page, so:

for word in urlopen(WORD_URL).readlines():
    WORDS.append(word.strip().decode('ascii'))

But in real life, you usually need to write code that reads the headers and dynamically figures it out. Or, better, install a higher-level library like requests, which does that for you automatically.

Explicitly convert byte type 'word' into string

result = result.replace("###", sre(word), 1)

it should work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!