1: Escape () function using the CGI module
>>>Import CGI>>> Stra ='<foo>\nfoo\t "Bar"'>>> Resa =Cgi.escape (stra)>>>print (RESA)<foo≫foo"Bar">>> STRB ="<foo>\nfoo\t ' Bar '">>> Resb =Cgi.escape (STRB)>>>print (RESB)<foo≫foo'Bar'
The single and double quotation marks in the visible string are not escaped as character entities, viewing the document
Escape (S, quote=None) "&" "<"" >" to html-safe sequences. is true, the quotation mark character (")is also translated.
If a quote=true is given, then the double quotes are escaped, but the single quotation marks do not;
RESAA = Cgi.escape (stra,true)>>> Print (RESAA)<foo>foo & quot;bar" #这里转义了双引号>>> resbb = cgi.escape (strb,true)>>> Print (RESBB)<foo >foo 'bar' # There's no escaping here
2: Escape method using HTML module
>>> Import HTML'<foo>\nfoo\t ' bar' >>> resa = Html.escape (stra)>>> Print (resa)<foo>foo "bar& quot; #这里做了转义处理 "<foo>\nfoo\t ' bar">>> resb = Html.escape (STRB)>>> Print (resb)<foo>foo
The escape method of the HTML module defaults to handling single and double quotes, the document:
Escape (S, Quote=true) Replace special characters " & " , < " and " > " to Html-safe sequences. If the optional flag quote is true ( The default ), the quotation mark characters, both double quote ( " ) and single quote (') characters is also translated.
Python-like PHP htmlspeialchars () filter String functions