print function in python3.1

Diez B. Roggisch deets at nospam.web.de
Mon Nov 23 14:16:08 CET 2009


Anjanesh Lekshminarayanan wrote:

> Python 3.1.1
> 
> sql = "INSERT INTO `tbl` VALUES (NULL, '%s', '%s', '%s', '%s', '%s');"
> for row in fp:
>     print (sql, (row[0],row[1],row[2],row[3],row[4]))
> .
> INSERT INTO `tbl` VALUES (NULL, '%s', '%s', '%s', '%s', '%s'); ('142',
> 'abc', '2006-04-09 02:19:24', '', '')
> .
> Why is it showing %s in the output ?

Because you gave it to it. How should it know that you want the %s to be
replaced with the other parameters? That's what the %-operator is for.

> 
> 1. I dont want to sql % () because that doesnt escape the strings
> 2. I cant use conn.escape_string(r) because Im not connected to a
> database. Output script to file and import to database loated
> elsewhere.

Depending on your DB-adapter, you are out of luck here. Either connect to a
db even if you don't need it, or try & see if you can locate the
implementation in the module somehow.

E.g. MySQLdb has the function exposed as "escape_string", not only on a
connection.

Diez




More information about the Python-list mailing list