Re: Problems replacing \ with \\



(top posting corrected)

En Mon, 21 Apr 2008 21:12:44 -0300, ockman@xxxxxxxxx <ockman@xxxxxxxxx> escribió:

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

> Failed example:
> escape(x)
> Expected:
> 'fun\\fun'
> Got:
> 'fun\x0cun'

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!

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?

r"This is a raw string"

"""
r"This is NOT a raw string"
"""

r"""
r"This is a raw string too"
"""

What matters are the OUTER quotes.

Now, why do you want to escape the text yourself? Assuming you have a DBAPI compatible module, use bound parameters when you execute the query:

cursor.execute("insert ... values (?,?,?)", (name, address, year))

Those ? are placeholders for the actual values; no explicit quoting on the values is required (the module itself, or the database, takes care of it). Not all databases support the ? style, there are other ways. See http://www.python.org/dev/peps/pep-0249/ for the DB API specification.

--
Gabriel Genellina

.



Relevant Pages

  • Re: Update existing values incrementally w/UPDATE SQL
    ... I think it would be safe to say, use the Dim statement any time you are ... Dim basically tells vba that you want to initiate a new variable. ... As far as quotes go, this was one of the trickiest ... Consider vba's interpretation of a string: ...
    (microsoft.public.access.modulesdaovba)
  • Re: Update existing values incrementally w/UPDATE SQL
    ... and in the book; Access 2007 VBA Programmer's Reference. ... As far as quotes go, this was one of the trickiest ... Consider vba's interpretation of a string: ... when an SQL is processed (I use an SQL example because it is the most ...
    (microsoft.public.access.modulesdaovba)
  • Re: Update existing values incrementally w/UPDATE SQL
    ... pretend that Me.txtString is a control on your form, ... You cant put Me.txtString inside the double quotes, or VBA just reads it as a ... and in the book; Access 2007 VBA Programmer's Reference. ... when an SQL is processed (I use an SQL example because it is the most ...
    (microsoft.public.access.modulesdaovba)
  • Re: SQL WHERE STMT PROBLEM
    ... is defined as a String variable, it is therefore not possible for it to ever ... possibly assign the value "False" to the BsSql variable. ... Dim oRst As DAO.Recordset ... > single and dbl quotes as instructed to produce the above BsSql ...
    (microsoft.public.access.queries)
  • Re: Multi Field SQL Where Clause
    ... Trying to DIM variables in a public function when the variables are already DIM'd will generate an error. ... I also struggled with creating the SQL string when I first started. ... Yes, it should have been a single quote, not three quotes. ... sysNtDvNo = sysPUOHDvNo ...
    (microsoft.public.access.forms)