php function "addslashes" / "removeslashes" in python

Ian Bicking ianb at colorstudy.com
Tue Jul 9 22:40:26 EDT 2002


I find these PHP functions rather crude -- proper encoding is
domain-specific, and it's not clear what the domain for addslashes is. 
Usually it is SQL, I suppose.  Most (all?) database adapters include an
encoding routine.

You can use repr() as a quoter, but it acts a little funny.  You could
use something like this:

def addslashes(s):
    return repr('"' + s)[2:-1].replace('"', '\\"')

This is fairly fast, but I don't know of a good way to decode this
(except eval, which I would strong suggest you not use).

  Ian







More information about the Python-list mailing list