Problems replacing \ with \\

Discussion in 'Python' started by samslists, Apr 21, 2008.

  1. samslists

    samslists Guest

    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 'fun\\\\fun'
    """
    string = string.replace('\\', '\\\\')
    return string

    Now the commands in the doctest work when I type them by hand into the
    python interpreter!'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!
     
    samslists, Apr 21, 2008
    #1
    1. Advertisements

  2. samslists

    MRAB Guest

    Your doctest is in a triple-quoted string which contains the line:
    ^^

    which is the same as:
    ^^^^

    If you wrap a raw string in just quotes that is isn't a raw string any
    longer!

    HTH
     
    MRAB, Apr 22, 2008
    #2
    1. Advertisements

  3. samslists

    ockman Guest

    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
     
    ockman, Apr 22, 2008
    #3
  4. Is it possible to use the database API and prepared statements to avoid
    having to go through this exercise? Also, most database APIs work
    natively in unicode, so creating your prepared statement and then
    passing in each parameter as an argument saves a lot of hassle, since
    the API's usually decode the unicode strings automatically.
     
    Michael Torrie, Apr 22, 2008
    #4
  5. (top posting corrected)

    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, Apr 22, 2008
    #5
  6. samslists

    samslists Guest

    Sorry for the late response. I've been travelling internationally and
    am just getting back to work.

    So--thank you to everyone who responded!

    To answer everyone's question---- I am dumping all of the data from a
    mysql database, then creating a postgresql database, then inserting
    the data into the new postgresql database.

    This seems to be a reasonable way to do it, because all I have to do
    is double my 's and my \s (I hope).

    Thanks
     
    samslists, May 3, 2008
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.