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,
Nothing to do with print()... Python 2.x would do the same thing...
PythonWin 2.5.2 (r252:60911, Mar 27 2008, 17:57:18) [MSC v.1310 32 bit
(Intel)] on win32.
Portions Copyright 1994-2006 Mark Hammond - see 'Help/About PythonWin'
for further copyright information.
row = tuple(range(5))
row (0, 1, 2, 3, 4)
sql = "INSERT INTO `data` VALUES (NULL,%s,%s,%s,%s,%s);"
print sql, (row[0],row[1],row[2],row[3],row[4])
INSERT INTO `data` VALUES (NULL,%s,%s,%s,%s,%s); (0, 1, 2, 3, 4)
vs
print sql % (row[0],row[1],row[2],row[3],row[4]) INSERT INTO `data` VALUES (NULL,0,1,2,3,4);
There is nothing magic about %s -- it is just a placeholder used by
operations that work on strings (and, since the MySQLdb adapter sends
strings, using Python's % operator, it is the placeholder also used by
the adapter).
You appear to be building a batch file of SQL insert statements...
What is your target database engine? OR: must you use Python 3.x? After
all -- for your apparent needs I wouldn't consider 3.x to be "production
ready".
Does Python 3.x include SQLite? It may be simpler to just use it to
create a scratch database, then use the command line program to dump the
scratch data as SQL (might need to post process to remove unwanted
statements).
E:\UserData\Dennis Lee Bieber\My Documents>sqlite3 test.db
SQLite version 3.6.3
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .dump test
BEGIN TRANSACTION;
CREATE TABLE test ( id primary key, stuff char );
INSERT INTO "test" VALUES(NULL,'one');
INSERT INTO "test" VALUES(NULL,'more');
INSERT INTO "test" VALUES(NULL,'one');
INSERT INTO "test" VALUES(NULL,'more');
INSERT INTO "test" VALUES(NULL,'test');
COMMIT;
sqlite> .dump stats
BEGIN TRANSACTION;
CREATE TABLE stats (id integer primary key,
name char(50),
num numeric);
INSERT INTO "stats" VALUES(1,'integer range',123);
INSERT INTO "stats" VALUES(2,'single range',3.14159265);
INSERT INTO "stats" VALUES(3,'single range int',31415926500);
INSERT INTO "stats" VALUES(4,'double range',5.12345676890123);
INSERT INTO "stats" VALUES(5,'double range int?',5.12345676890124e+20);
COMMIT;
sqlite>
Otherwise, you are going to have write the equivalent function as
MySQLdb's escape_strings(), which replaces dangerous characters (oh, it
also converts all data types to string first) with escaped versions --
and THEN wraps the string with the proper quotes.