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

前端 未结 3 1088
故里飘歌
故里飘歌 2020-12-02 17:09

I am working on exercise 41 in learnpythonthehardway and keep getting the error:

  Traceback (most recent call last):
  File \".\\url.py\", line 72, in <         


        
相关标签:
3条回答
  • 2020-12-02 17:20

    Explicitly convert byte type 'word' into string

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

    it should work

    0 讨论(0)
  • 2020-12-02 17:36

    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?

    0 讨论(0)
  • 2020-12-02 17:46

    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.

    0 讨论(0)
提交回复
热议问题