Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 74667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:23:39+00:00 2026-05-10T20:23:39+00:00

I have a string that is HTML encoded: ”’<img class="size-medium wp-image-113"\ style="margin-left: 15px;" title="su1"\

  • 0

I have a string that is HTML encoded:

'''<img class="size-medium wp-image-113"\  style="margin-left: 15px;" title="su1"\  src="http://blah.org/wp-content/uploads/2008/10/su1-300x194.jpg"\  alt="" width="300" height="194" />''' 

I want to change that to:

<img class='size-medium wp-image-113' style='margin-left: 15px;'    title='su1' src='http://blah.org/wp-content/uploads/2008/10/su1-300x194.jpg'    alt='' width='300' height='194' />  

I want this to register as HTML so that it is rendered as an image by the browser instead of being displayed as text.

The string is stored like that because I am using a web-scraping tool called BeautifulSoup, it ‘scans’ a web-page and gets certain content from it, then returns the string in that format.

I’ve found how to do this in C# but not in Python. Can someone help me out?

Related

  • Convert XML/HTML Entities into Unicode String in Python
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T20:23:39+00:00Added an answer on May 10, 2026 at 8:23 pm

    Given the Django use case, there are two answers to this. Here is its django.utils.html.escape function, for reference:

    def escape(html):     '''Returns the given HTML with ampersands, quotes and carets encoded.'''     return mark_safe(force_unicode(html).replace('&', '&amp;').replace('<', '&l t;').replace('>', '&gt;').replace(''', '&quot;').replace(''', '&#39;')) 

    To reverse this, the Cheetah function described in Jake’s answer should work, but is missing the single-quote. This version includes an updated tuple, with the order of replacement reversed to avoid symmetric problems:

    def html_decode(s):     '''     Returns the ASCII decoded version of the given HTML string. This does     NOT remove normal HTML tags like <p>.     '''     htmlCodes = (             (''', '&#39;'),             (''', '&quot;'),             ('>', '&gt;'),             ('<', '&lt;'),             ('&', '&amp;')         )     for code in htmlCodes:         s = s.replace(code[1], code[0])     return s  unescaped = html_decode(my_string) 

    This, however, is not a general solution; it is only appropriate for strings encoded with django.utils.html.escape. More generally, it is a good idea to stick with the standard library:

    # Python 2.x: import HTMLParser html_parser = HTMLParser.HTMLParser() unescaped = html_parser.unescape(my_string)  # Python 3.x: import html.parser html_parser = html.parser.HTMLParser() unescaped = html_parser.unescape(my_string)  # >= Python 3.5: from html import unescape unescaped = unescape(my_string) 

    As a suggestion: it may make more sense to store the HTML unescaped in your database. It’d be worth looking into getting unescaped results back from BeautifulSoup if possible, and avoiding this process altogether.

    With Django, escaping only occurs during template rendering; so to prevent escaping you just tell the templating engine not to escape your string. To do that, use one of these options in your template:

    {{ context_var|safe }} {% autoescape off %}     {{ context_var }} {% endautoescape %} 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string that contains HTML image elements that is stored in a
Background: I have string of html with about 10 image tags that passes through
If I have a string that contains the html from a page I just
I have a string of text that contains html, and I need to extract
i have string stored in python variables, and i am outputting a html that
I have a AJAX request that will return a string that contains some HTML
I have a property in my backing bean that returns html code: public String
I have a string, that may or may not be valid HTML , but
I have a base64 encoded string that I would like to convert into an
I have a string that represents an HTML block. All the unicode characters are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.