In the last lesson,
we managed to get our API to work and we got back some live data.
But when we ran it,
we saw that some of the texts that we were getting back was formatted really
strangely with these pound signs and ampersands,
and it's not the actual text that we see.
So what's happening here? Well,
what we're actually seeing here are called HTML entities,
and there are a way of replacing certain characters in HTML so that it doesn't
get confused with HTML code. So for example,
the less than symbol could be a part of HTML code.
And instead of using that, we have to use the
< and then semicolon.
So if we look down this table,
we can actually see this " actually stands for a double quotation mark.
And that would make sense cause it's saying "Mario Kart
64"
and this #039,
if we look up in this list, is actually a single quotation mark.
And that would make sense as well, cause it would be Stalin's death.
So how do we get hold of the actual human readable text? Well,
we can use this tool called the free formatter to
unescape the HTML results that we're getting back from our API.
I've copied and pasted this part we've got here.
And if I go ahead and click on unescape,
you can see that it formats it into the original human readable format
and now it says in "Mario Kart 64" Waluigi is a playable character.
And if I paste the cold war ended with Joseph Stalin, blah, blah, blah,
death and I click unescape, then you can see
it says the Cold War ended with Joseph Stalin's death and it replaces that with
an apostrophe. Now,
essentially we know what to Google and that's kind of the first step towards
solving any problem.
So if you Google for escaping HTML entities in Python,
then the first result we get in Stack Overflow gives us the answer.
We have to import the HTML module and use one of the methods in that module
called unescape in order to unescape the text that we're getting back.
The part where we're interested in this is in our quiz brain,
because that's the part what we format it into our user answer.
Let's change the question
text to be equal to the self.current_question.text,
so this part that we have here
which is being put into our input and we can use this q_text instead.
But instead of using just the text that we get back from the API,
we're going to import the HTML module.
1: Yeah.
0: And we're going to use the method inside this HTML module called unescape
to unescape this string that we get from the API.
And now if I run this code again, you can see that this time,
no matter what is inside the string, say an apostrophe in this case or a double
quote in this case, they're all being formatted correctly.
There you have it.
We started off with some strange characters and after a bit of Googling around,
we found the solution to turn them into human readable text. Aas a programmer,
this is a skill that you have to really hone.
This is something that is going to take you to the next level
to this intermediate++ level.
You have to find out solutions to your own problems,
and Google is your best friend.