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

前端 未结 3 1103
故里飘歌
故里飘歌 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: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?

提交回复
热议问题