[Tutor] CSV file processing...

Spencer Parker inthefridge at gmail.com
Mon Mar 24 15:51:25 CET 2008


I am at a loss as to why this isn't working properly:

#!/usr/bin/python

import MySQLdb
import csv
import sys

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

co = db.cursor()
csv_data = csv.reader(file('output.txt','r'))
headers = csv_data.next()
row = csv_data.next()
prefix = row[:17]
for i in range(17, len(row),8):
    next = prefix + row[i:i+8]
    print next
co.execute("""
    INSERT INTO stats(Hostname, Time, Arch, PhysicalCPU, Count, Running,
Blocked, Paused, Shutdown, Shutoff, Crashed, Active, Inactive, PCPU,
TotalMemory, Totalguestmemory, TotalCPUtime, DomainID, Domainname, CPUTime,
PGCPU, RDRQ, WRRQ, RXBY, TXBY)

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()
db.commit()
db.close()

It pulls all of the data correctly and it is the correct of items that it is
grabbing.  I get this traceback:

Traceback (most recent call last):
  File "./loadcsv.py", line 21, in ?
    co.execute("""
  File "/usr/lib64/python2.4/site-packages/MySQLdb/cursors.py", line 148, in
execute
    query = query % db.literal(args)
TypeError: not all arguments converted during string formatting

If I have it print out the next variable...the data is correct that it is
pulling out...

['test.test.net', '15:33:59', 'x86_64', '8', '9', '1', '4', '0', '0', '0',
'0', '5', '4', '0.0', '12495360', '818688', '0', '1', 'beta', '0.', '0.',
'', '', '', '']

Maybe fresh eyes could pull out my mistake?

Thanks again for everything!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20080324/747080e3/attachment.htm 


More information about the Tutor mailing list