[Tutor] CSV file processing...

Eric Walstad eric at ericwalstad.com
Fri Mar 21 17:01:31 CET 2008

Hey Spencer,

Spencer Parker wrote:
> This is why I should not be allowed to write emails at the end of the 
> day.  At least ones that need deep explanation.  Let's try this a second 
> time.
Thanks, this looks better, but...

> This is what I have now...
*What isn't working* the way you want?  Traceback?  I'll guess that you 
don't want to have to hardcode the columns for a fixed number of virtual 
machine data into your sql string...

> try:
>     co = db.cursor()
>     csv_data = csv.reader(file('output.txt','r'))
>     for row in csv_data: print row
>     co.execute("""
>         INSERT INTO stats (Hostname, Time, Arch, PhysicalCPU, Count, 
> Running, Blocked, Paused, Shutdown, Shutoff, Crashed, Active, Inactive, 
> PCPU, TotalMemory, Totalguestmemory, TotalCPUtime, DomainID, Domainname, 
>         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()
Huh?  I was expecting to see 27 fields (20 for the host machine and 7 
for the virtual machine), you have 25 above.  Yes, a traceback or 
explanation of what's going wrong would be helpful.

Other things I'd recommend are:

1. I'm pretty sure that your 'row' above is a list of values. 
Investigate Python's list slicing operator[1] that other responses to 
your first email have used.  Doing so will help you understand...

2. Kent's response to your first email looks great.  Reread it and I 
think you'll find it does what you want (handle a variable number of 
virtual machines and their data).  See also: the range function and it's 
'step' argument[2].

3. Nice that you are sending the 'row' list to the database adapter and 
letting it escape the values.  In your case I don't think you have to 
worry about SQL injection, but it's a good habit to get into if you ever 
need to stuff end-user data into a database.


[2] http://docs.python.org/tut/node6.html#SECTION006300000000000000000

More information about the Tutor mailing list