[Tutor] CSV file processing...

Spencer Parker inthefridge at gmail.com
Fri Mar 21 20:24:30 CET 2008


I posted it in a previous message on the list...but here it is...

#!/usr/bin/python

import MySQLdb
import csv
import sys

try:
    db = MySQLdb.connect (host = "localhost",user = "root",passwd =
"Ch33s3Monk3y",db = "xenstats")
except MySQLdb.Error, e:
    print "Error %d: %s" % (e.args[0], e.args[1])
    sys.exit (1)

try:
    co = db.cursor()
    csv_data = csv.reader(file('output.txt','r'))
    headers = csv_data.next()
    row = csv_data.next()
    prefix = row[:20]
    for i in range(20, len(row), 7):
        next = prefix + row[i:i+7]

        for row in csv_data: print next
        co.execute("""
            INSERT INTO stats VALUES
(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);
        """,row)

        co.close()

except MySQLdb.Error, e:
    print "Error %d: %s" % (e.args[0], e.args[1])
    sys.exit (1)

db.commit()
db.close()

On Fri, Mar 21, 2008 at 1:18 PM, Kent Johnson <kent37 at tds.net> wrote:

> Spencer Parker wrote:
> > Okay...so I got it mostly working...it now dies with this trackback:
>
> It would be helpful to see what 'it' is (i.e. your code), and also to
> see the actual error message as well as the traceback. We try to read
> minds on this group but we aren't really very good at it.
>
> Kent
>
> >
> > Traceback (most recent call last):
> >   File "./loadcsv.py", line 23, in ?
> >     co.execute("""
> >   File "/usr/lib64/python2.4/site-packages/MySQLdb/cursors.py", line
> > 148, in execute
> >     query = query % db.literal(args)
>



-- 
Spencer Parker
_______________________________________________________

"if you can't go to heaven, may you at least die in Ireland."

_______________________________________________________
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20080321/48788fb8/attachment.htm 


More information about the Tutor mailing list