Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape html in python?

i have a <img src=__string__> but string might contain ", what should I do to escape it?

Example:

__string__ = test".jpg
<img src="test".jpg">

doesn't work.

like image 268
Timmy Avatar asked Jun 22 '10 20:06

Timmy


1 Answers

In Python 3.2 a new html module was introduced, which is used for escaping reserved characters from HTML markup.

It has one function html.escape(s, quote=True). If the optional flag quote is true, the characters (") and (') are also translated.

Usage:

>>> import html
>>> html.escape('x > 2 && x < 7')
'x &gt; 2 &amp;&amp; x &lt; 7'
like image 141
Maciej Ziarko Avatar answered Oct 11 '22 22:10

Maciej Ziarko