Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > quoting %

Reply
Thread Tools

quoting %

 
 
Matthias Teege
Guest
Posts: n/a
 
      10-13-2004

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
 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      10-13-2004
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>



 
Reply With Quote
 
 
 
 
Thomas Guettler
Guest
Posts: n/a
 
      10-13-2004
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,

%% --> %



 
Reply With Quote
 
Richie Hindle
Guest
Posts: n/a
 
      10-13-2004

[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


 
Reply With Quote
 
Cliff Wells
Guest
Posts: n/a
 
      10-13-2004
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 <>

 
Reply With Quote
 
Andreas Kostyrka
Guest
Posts: n/a
 
      10-13-2004
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

 
Reply With Quote
 
Dan Sommers
Guest
Posts: n/a
 
      10-15-2004
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.
 
Reply With Quote
 
Gandalf
Guest
Posts: n/a
 
      10-15-2004

>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


 
Reply With Quote
 
Dan Sommers
Guest
Posts: n/a
 
      10-16-2004
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What are all these nonesence sites quoting mine but with no content? xyZed HTML 8 04-05-2006 04:52 PM
Quoting literals properly yoni XML 12 02-18-2006 10:42 PM
Quoting original messages in replies John Coxon Firefox 14 04-24-2004 06:18 PM
Outlook Express quoting NetNews Computer Support 4 11-16-2003 08:10 AM
Quoting confusion Derek Fountain XML 2 08-13-2003 10:36 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57