473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

quoting %


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
Jul 18 '05 #1
8 1493
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,

%% --> %

Jul 18 '05 #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>

Jul 18 '05 #3

[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
ri****@entrian.com

Jul 18 '05 #4
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 <cl************@comcast.net>

Jul 18 '05 #5
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

Jul 18 '05 #6
On Wed, 13 Oct 2004 16:27:10 +0200,
Andreas Kostyrka <an*****@kostyrka.org> 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.
Jul 18 '05 #7
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
Jul 18 '05 #8
On Fri, 15 Oct 2004 22:15:15 +0200,
Gandalf <ga*****@geochemsource.com> 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.
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: David M. Wilson | last post by:
Hello fellow users! I've been using Python in a couple of different environments for a few years now. I have quite often found that I have needed to deal with filenames which may contain...
2
by: snacktime | last post by:
I'm used to using the perl DBI and not very familiar with the python DB-API. I am using PyGreSQL. My question is what is the standard way to quote strings in sql queries? I didn't see any...
22
by: Jane Withnolastname | last post by:
I have been using the charset windows-1252 for a while, but it was pointed out to someone else in this group recently that it's a Microsoft creation (I'm sure I'm getting my facts wrong or skewed)...
21
by: Headless | last post by:
I've marked up song lyrics with the <pre> tag because it seems the most appropriate type of markup for the type of data. This results in inefficient use of horizontal space due to UA's default...
383
by: John Bailo | last post by:
The war of the OSes was won a long time ago. Unix has always been, and will continue to be, the Server OS in the form of Linux. Microsoft struggled mightily to win that battle -- creating a...
27
by: Ron Adam | last post by:
Hi, I found the following to be a useful way to access arguments after they are passed to a function that collects them with **kwds. class namespace(dict): def __getattr__(self, name): return...
4
by: Tim Robinson | last post by:
Hi, I generally consider myself competent with the complexities of quoting but I can't figure out postgres at all. I've read the manual and it looks very straightforward but that doesn't accord...
10
by: Michael Hoffman | last post by:
Does anyone have a script to convert more conventional USENET quoting style like this: John wrote: > Jacob Jingleheimer Schmidt <jjs@example.net> wrote in message-id <gratuitous-detail> on 29...
82
by: Edward Elliott | last post by:
This is just anecdotal, but I still find it interesting. Take it for what it's worth. I'm interested in hearing others' perspectives, just please don't turn this into a pissing contest. I'm in...
7
by: Steven W. Orr | last post by:
Python has a number of "quoting" 'options' to help """with times when""" one way may be more convenient than another. In the world of shell scripting, I use a technique that I call minimal...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.