Problems replacing \ with \\

samslists at gmail.com samslists at gmail.com
Mon Apr 21 18:48:40 EDT 2008


Hi...

Here's a weird problem...I'm trying to escape a bunch of data to put
into a database.

Here's what I have:

def escape(string):
    """
    Escape both single quotes and blackslashes
    >>> x = r"fun\fun"
    >>> escape(x)
    'fun\\\\fun'
    """
    string = string.replace('\\', '\\\\')
    return string

Now the commands in the doctest work when I type them by hand into the
python interpreter!
>>> x = r"fun\fun"
>>> escape(x)
'fun\\\\fun'


But they don't work when I actually run them with doctest:
Failed example:
    escape(x)
Expected:
    'fun\\fun'
Got:
    'fun\x0cun'

Why?

Thanks!



More information about the Python-list mailing list