quoting %

Discussion in 'Python' started by Matthias Teege, Oct 13, 2004.

  1. Moin,

    I need a string like this "foo '%value%'" for a database query but
    print "'%%s%'"%value gives me TypeError: not all arguments converted
    during string formatting. How does I quote the "%" sign in this case?

    Matthias
    --
    Matthias Teege -- http://www.mteege.de
    make world not war
    Matthias Teege, Oct 13, 2004
    #1
    1. Advertising

  2. Matthias Teege wrote:

    > I need a string like this "foo '%value%'" for a database query but
    > print "'%%s%'"%value gives me TypeError: not all arguments converted
    > during string formatting. How does I quote the "%" sign in this case?


    use "%%".

    for the full story, see:

    http://docs.python.org/lib/typesseq-strings.html

    </F>
    Fredrik Lundh, Oct 13, 2004
    #2
    1. Advertising

  3. Am Wed, 13 Oct 2004 13:32:38 +0200 schrieb Matthias Teege:

    >
    > Moin,
    >
    > I need a string like this "foo '%value%'" for a database query but
    > print "'%%s%'"%value gives me TypeError: not all arguments converted
    > during string formatting. How does I quote the "%" sign in this case?


    Hi,

    %% --> %
    Thomas Guettler, Oct 13, 2004
    #3
  4. [Matthias]
    > I need a string like this "foo '%value%'" for a database query but
    > print "'%%s%'"%value gives me TypeError: not all arguments converted
    > during string formatting. How does I quote the "%" sign in this case?


    Double up the percent signs:

    >>> print "foo '%%%s%%'" % 'spam'

    foo '%spam%'

    --
    Richie Hindle
    Richie Hindle, Oct 13, 2004
    #4
  5. Matthias Teege

    Cliff Wells Guest

    On Wed, 2004-10-13 at 13:32 +0200, Matthias Teege wrote:
    > Moin,
    >
    > I need a string like this "foo '%value%'" for a database query but
    > print "'%%s%'"%value gives me TypeError: not all arguments converted
    > during string formatting. How does I quote the "%" sign in this case?


    You need to escape the extra percents:

    print "'%%%s%%'" % value

    Regards,
    Cliff

    --
    Cliff Wells <>
    Cliff Wells, Oct 13, 2004
    #5
  6. On Wed, Oct 13, 2004 at 01:32:38PM +0200, Matthias Teege wrote:
    >
    > Moin,
    >
    > I need a string like this "foo '%value%'" for a database query but
    > print "'%%s%'"%value gives me TypeError: not all arguments converted
    > during string formatting. How does I quote the "%" sign in this case?

    Try: "'%%%s%%'"
    And if your not completly sure what type value might be, it's better
    to use str % (value,) (Hint: what happens if type(value) == tuple)?

    Andreas
    >
    > Matthias
    > --
    > Matthias Teege -- http://www.mteege.de
    > make world not war
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    Andreas Kostyrka, Oct 13, 2004
    #6
  7. Matthias Teege

    Dan Sommers Guest

    On Wed, 13 Oct 2004 16:27:10 +0200,
    Andreas Kostyrka <> wrote:

    > On Wed, Oct 13, 2004 at 01:32:38PM +0200, Matthias Teege wrote:


    >> Moin,


    >> I need a string like this "foo '%value%'" for a database query but
    >> print "'%%s%'"%value gives me TypeError: not all arguments converted
    >> during string formatting. How does I quote the "%" sign in this case?


    > Try: "'%%%s%%'"


    Be careful with the quotation marks, too; consider the case that value
    contains a quotation mark (or an apostrophe).

    I don't know what kind of database and/or API you're using, but DB-API
    compliant (Python PEP 249) libraries will put the right quotation marks
    (if any) around your strings *and* quote any internal troublesome
    characters correctly.

    HTH,
    Dan

    --
    Dan Sommers
    <http://www.tombstonezero.net/dan/>
    Never play leapfrog with a unicorn.
    Dan Sommers, Oct 15, 2004
    #7
  8. Matthias Teege

    Gandalf Guest


    >I don't know what kind of database and/or API you're using, but DB-API
    >compliant (Python PEP 249) libraries will put the right quotation marks
    >(if any) around your strings *and* quote any internal troublesome
    >characters correctly.
    >

    Can you please send an example? I'm using the DB-API extensively, but I
    have my own conversion
    functions for date/time types. It would be great to have a general method.

    Laci 2.0
    Gandalf, Oct 15, 2004
    #8
  9. Matthias Teege

    Dan Sommers Guest

    On Fri, 15 Oct 2004 22:15:15 +0200,
    Gandalf <> wrote:

    >> I don't know what kind of database and/or API you're using, but
    >> DB-API compliant (Python PEP 249) libraries will put the right
    >> quotation marks (if any) around your strings *and* quote any internal
    >> troublesome characters correctly.


    > Can you please send an example? I'm using the DB-API extensively, but
    > I have my own conversion functions for date/time types. It would be
    > great to have a general method.


    Here's a snippent from one of my programs:

    def delete( self, tablename, column_names, row ):
    cursor = self.connection.cursor( )
    sql = ("DELETE FROM %s WHERE " % tablename
    + column_names[ 0 ] + " = %s")
    params = tuple( row[ :1 ] )
    cursor.execute( sql, params ) # <-- look here
    self.connection.commit( )

    By the time it gets to the "look here" line, sql looks like this:

    DELETE FROM tablename WHERE columnname = %s

    (but tablename and columnname are actual table and column names). Note
    that cursor.execute quotes the rest of the WHERE clause *correctly*,
    even if params contains quote or percent or whatever characters.

    FWIW, I would probably be a bit more bold nowadays, and construct sql
    and params more like this:

    sql = "DELETE FROM %(table)s WHERE %(column)s = %(target)s"
    params = { 'table' : tablename,
    'column' : column_names[ 0 ],
    'target' : row[ :1 ] }

    assuming that the database module in question supports the pyformat
    paramstyle, or like this:

    sql = "DELETE FROM %s WHERE %s = %s"
    params = (tablename, column_names[ 0 ], row[ :1 ])

    if it didn't.

    See also PEP 249 <http://www.python.org/peps/pep-0249.html>, especially
    the bit about paramstyle and cursor.execute (and footnotes 2 and 5).

    HTH,
    Dan

    --
    Dan Sommers
    <http://www.tombstonezero.net/dan/>
    Never play leapfrog with a unicorn.
    Dan Sommers, Oct 16, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. xyZed
    Replies:
    8
    Views:
    373
    xyZed
    Apr 5, 2006
  2. Derek Fountain

    Quoting confusion

    Derek Fountain, Aug 13, 2003, in forum: XML
    Replies:
    2
    Views:
    1,294
    Derek Fountain
    Aug 13, 2003
  3. yoni
    Replies:
    12
    Views:
    794
  4. jsnX

    quoting quotes, &c.

    jsnX, Apr 5, 2005, in forum: C++
    Replies:
    1
    Views:
    352
    Victor Bazarov
    Apr 5, 2005
  5. David M. Wilson
    Replies:
    6
    Views:
    336
    David M. Wilson
    Dec 14, 2003
Loading...

Share This Page