4

I have a sentence

sentence =  <p> Reading, watching or <span class="matching">listening</span> to the media isn’t <span class="matching">matching</span><span class="matching">much</span> help either. </p>

to make it render properly at front-end here is what I have done

from flask import Markup
sentence = Markup(sentence)

But the output is only rendered properly for one markup (not necessarily first one) and others are not rendered.

            <p> Reading, watching or <span class="matching">listening</span> to the media isn’t &lt;span class=&#34;matching&#34;&gt;much&lt;/span&gt; help either. </p>

What am I doing it wrong here?

3
  • In django we would do something like this: {{ variable_name|safe }} Commented Aug 6, 2013 at 10:14
  • I got following error when I tried your example in terminal: "UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 81: ordinal not in range(128)" Didn't you get such error? Here is the SO link to solve it: stackoverflow.com/questions/1342000/… Commented Aug 6, 2013 at 11:35
  • Thanks @rajpy the link you posted has an function which removes all the non ascii characters and it makes it easier. Thanks :) Commented Aug 6, 2013 at 12:09

1 Answer 1

6

The culprit is the

isn’t

that "’" is not valid ASCII, because of which it doesn't come in the valid range of characters of HTML Markup , thus it escapes it (though it should throw an error)

Hope that solves the issue.

This works for me

from flask import Markup
sentence =  '<p> Reading, watching or <span class="matching">listening</span> to the media isn\'t <span class="matching">matching</span><span class="matching">much</span> help either. </p>'
Markup(sentence)

returns

Markup(u'<p> Reading, watching or <span class="matching">listening</span> to the media isn\'t <span class="matching">matching</span><span class="matching">much</span> help either. </p>')

hope that is what is the required output

Sign up to request clarification or add additional context in comments.

2 Comments

ok. You see the culprit and you have manually edited and added escape to make it valid HTML Markup. But I have gazillions of sentences. How do you first change those sentence to valid HTML Markup?
well, you can write a script to either remove all characters out of range .. or replace them with a valid one .. :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.