Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unescape apostrophes and such in Python?

I have a string with symbols like this:

'

That's an apostrophe apparently.

I tried saxutils.unescape() without any luck and tried urllib.unquote()

How can I decode this? Thanks!

like image 899
rick Avatar asked May 03 '09 03:05

rick


People also ask

How do you do an apostrophe in Python?

By using the escape character \" we are able to use double quotes to enclose a string that includes text quoted between double quotes. Similarly, we can use the escape character \' to add an apostrophe in a string that is enclosed in single quotes: print('Sammy\'s balloon is red. ')

How does Python deal with apostrophes?

I can see 2 easy ways out: 1) Enclose the string in double quotes, so you can use apostrophes inside. example: "BRIAN'S MOTHER". 2)Use the "\" escape character to escape the apostrophe. example: "BRIAN\'S MOTHER".


1 Answers

Check out this question. What you're looking for is "html entity decoding". Typically, you'll find a function named something like "htmldecode" that will do what you want. Both Django and Cheetah provide such functions as does BeautifulSoup.

The other answer will work just great if you don't want to use a library and all the entities are numeric.

like image 151
easel Avatar answered Sep 23 '22 16:09

easel