A critique of cgi.escape

Scott David Daniels scott.daniels at acm.org
Sun Oct 8 20:13:17 CEST 2006


Lawrence D'Oliveiro wrote:
> Another useful function is this:
> 
>     def JSString(Str) :
>         """returns a JavaScript string literal that evaluates to Str....

You can do this more simply:

     _map = {"\\" : "\\\\", "\"" : "\\\"", "\t" : "\\t", "\n" : "\\n"}
     def JSString(Str) :
         mapped = [_map.get(Ch, Ch) for Ch in Str]
         return "\"" + "".join(mapped) + "\""



-- 
--Scott David Daniels
scott.daniels at acm.org



More information about the Python-list mailing list