So I have this string 1993 – Liam Payne, English singer-songwriter[17] that you can see contains the characters &# followed by a number. How can I delete them automatically from my string, like you can do with \u1321 type chars for example? I tried using .encode("ascii", "ignore") and .decode() but with no success.
Thanks in advance!
>Solution :
Python 3.4+
Use html.unescape():
import html
print(html.unescape('£682m'))
FYI html.parser.HTMLParser.unescape is deprecated, and was supposed to be removed in 3.5, although it was left in by mistake. It will be removed from the language soon.
Python 2.6-3.3
You can use HTMLParser.unescape() from the standard library:
- For Python 2.6-2.7 it’s in
HTMLParser - For Python 3 it’s in
html.parser
>>> try:
... # Python 2.6-2.7
... from HTMLParser import HTMLParser
... except ImportError:
... # Python 3
... from html.parser import HTMLParser
...
>>> h = HTMLParser()
>>> print(h.unescape('£682m'))
£682m
You can also use the six compatibility library to simplify the import:
>>> from six.moves.html_parser import HTMLParser
>>> h = HTMLParser()
>>> print(h.unescape('£682m'))
£682m