Monday, June 27, 2011

Make string html-safe

To make a string HTML-safe (convert & to &, > to > and so forth):

>>> import cgi
>>>
>>> test_string = "This is <unsafe> string & it contains wrong symbols"
>>>
>>> print cgi.escape(test_string)
This is &lt;unsafe&gt; string &amp; it contains wrong symbols

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.