print function in python3.1

Diez B. Roggisch deets at nospam.web.de
Mon Nov 23 17:07:33 CET 2009


Anjanesh Lekshminarayanan wrote:

>> Maybe it would help if you explained what you are actually trying to
>> accomplish.
> 
> import csv
> f = csv.reader(open('data.txt'), delimiter='\t') # 2GB text file
> sql = "INSERT INTO `data` VALUES (NULL,%s,%s,%s,%s,%s);"
> for row in f:
>     print (sql, (row[0],row[1],row[2],row[3],row[4]))
> 
> $ python3 parse.py3 > data.sql
> 
> But because of print() being a function in Py3,
> print (sql, (row[0],row[1],row[2],row[3],row[4]))
> prints
> INSERT INTO `data` VALUES (NULL, '%s', '%s', '%s', '%s', '%s');
> ('142', 'abcde', '2006-03-01 05:17:14', '', '')
> instead of
> INSERT INTO `data` VALUES (NULL, '142', 'abcde', '2006-03-01 05:17:14',
> '', '');

No. It does so because you don't use 

  print(sql % (row[0],row[1],row[2],row[3],row[4]))

And that has nothing to do with print being a function or not.

Diez





More information about the Python-list mailing list