I am working on exercise 41 in learnpythonthehardway and keep getting the error:
Traceback (most recent call last):
File \".\\url.py\", line 72, in <
Explicitly convert byte type 'word' into string
result = result.replace("###", sre(word), 1)
it should work
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.