dummy needs help with Python

Rick van Hattem Rick at youtellme.com
Fri Dec 26 04:17:19 CET 2008


On Friday 26 December 2008 03:24:41 MLDSpenser at aol.com wrote:
> I don't have time to work my way through the online Python tutorial. I've
> tried a couple of forums but nobody has answered my questions.

There are loads of Python tutorials on the net, some will teach you the basics 
in 5 minutes, others will give you a more thorough understanding in a couple 
of days. But it shouldn't be that hard I'd say, if you want it fast than I'd 
recommend this one: http://hetland.org/writing/instant-python.html

> A simple 
> program with comments that say "here's where I read File A and define/map/
> the fields in it" would let me learn by testing and trying things out, the
> same way I learned to work with the fields in the module.

Here's a little example program, it's actually quite simple ;)
import csv

csv_reader = csv.reader(open('spam.csv'))

for row in csv_reader:
  print row

That will print all the rows in the file, if you want to extract the specific 
fields you could do something like this:
for a, b, c in csv_reader:
  print a, b, c

Where a, b and c are the fields (I'd recommend a more useful name though).

For writing a csv the process is similar, but open it like this and use 
writerow:
csv_writer = csv.writer(open('eggs.csv', 'w'))

Hopefully that helps :)



More information about the Python-list mailing list