Search code examples
pythontext-to-html

Python convert html tags in ampersand format


I have text in the following format ampersand lt;div ampersand gt;\n ampersand lt;div ampersand gt; ampersand lt;span ampersand gt;Customer.. How can i convert this text to html i.e.

<div>
<div>
<span>
Customer..

Solution

  • Try:

    import html
    html_string = "ampersand lt;div ampersand gt;\n ampersand lt;div ampersand gt; ampersand lt;span ampersand gt;Customer..".replace("ampersand ", "&")
    html.unescape(html_string)
    

    In case you are looking to maintain new lines:

    html.unescape(html_string.replace("\n", "<br />"))