[Tutor] CSV file processing...

Spencer Parker inthefridge at gmail.com
Mon Mar 24 16:38:09 CET 2008


Okay...that got me a different error:

Traceback (most recent call last):
  File "./loadcsv.py", line 21, in ?
    co.execute("""
  File "/usr/lib64/python2.4/site-packages/MySQLdb/cursors.py", line 163, in
execute
    self.errorhandler(self, exc, value)
  File "/usr/lib64/python2.4/site-packages/MySQLdb/connections.py", line 35,
in defaulterrorhandler
    raise errorclass, errorvalue
_mysql_exceptions.OperationalError: (1136, "Column count doesn't match value
count at row 1")

Thanks for getting me past that part...once this is error is resolved...then
it should be done.  The good thing is I think I could actually do this again
and understand what I am doing...I guess there is progress there.

On Mon, Mar 24, 2008 at 9:27 AM, Kent Johnson <kent37 at tds.net> wrote:

> Spencer Parker wrote:
> > 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)
>
> You want 'next', not 'row'
>
> Kent
>
> > 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 <http://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!
> >
> >
> > ------------------------------------------------------------------------
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
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/20080324/e2458817/attachment-0001.htm 


More information about the Tutor mailing list