Problems replacing \ with \\

ockman at gmail.com ockman at gmail.com
Mon Apr 21 20:12:44 EDT 2008


HTH --

Thank you for the response.  I'm not sure I understand the last
sentence, although I think I get the idea.  How do I create a proper
doctest?

Thanks

On Apr 21, 9:08 pm, MRAB <goo... at mrabarnett.plus.com> wrote:
> On Apr 21, 11:48 pm, "samsli... at gmail.com" <samsli... at gmail.com>
> wrote:
>
>
>
> > 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!
>
> Your doctest is in a triple-quoted string which contains the line:
>
> >>> x = r"fun\fun"
>
>              ^^
>
> which is the same as:
>
> >>> x = r"fun\x0cun"
>
>              ^^^^
>
> If you wrap a raw string in just quotes that is isn't a raw string any
> longer!
>
> HTH




More information about the Python-list mailing list