[Tutor] CSV file processing...

Kent Johnson kent37 at tds.net
Fri Mar 21 20:34:14 CET 2008


Spencer Parker wrote:
> I posted it in a previous message on the list...but here it is...

Looks new to me...
> 
> #!/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

Why do you have this loop? You only want to process one row, right?

>         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)

Should be next - the processed data - not row - the raw data.

You still didn't show the actual error message. After the traceback 
there should be an error.

Kent


More information about the Tutor mailing list