csv reading fields

M

Mark F

To say I'm a python newbie would be an understatement but I think it is a
language worth learning and would make a great replacement for perl in our
shop.

I'm trying to read a CSV file using the python csv module but I can't seem
to determine how to use the API, it seems to me that I can do nothing more
than opening the file and printing the lines of the file. I can do that
without the CSV module. I need to know how to address individual fields.

Something like this: (not actual python code)


import csv
reader = csv.reader(file("some.csv"))
for row in reader:
print row.field(3) or import csv
reader = csv.reader(file("some.csv"))
if reader.field(2,1) == "test.zip": print "test.zip exists"
Thanks,-Mark
 
N

Nick Smallbone

Mark F said:
To say I'm a python newbie would be an understatement but I think it is a
language worth learning and would make a great replacement for perl in our
shop.

I'm trying to read a CSV file using the python csv module but I can't seem
to determine how to use the API, it seems to me that I can do nothing more
than opening the file and printing the lines of the file. I can do that
without the CSV module. I need to know how to address individual fields.

Something like this: (not actual python code)


import csv
reader = csv.reader(file("some.csv"))
for row in reader:
print row.field(3) or import csv
reader = csv.reader(file("some.csv"))
if reader.field(2,1) == "test.zip": print "test.zip exists"
Thanks,-Mark

How about:

import csv
reader = csv.reader(file("some.csv"))
for row in reader:
print row[3]

Nick
 
M

Mark F

Thanks,
could have sworn I tried that.

-Mark

Nick Smallbone said:
Mark F said:
To say I'm a python newbie would be an understatement but I think it is a
language worth learning and would make a great replacement for perl in our
shop.

I'm trying to read a CSV file using the python csv module but I can't seem
to determine how to use the API, it seems to me that I can do nothing more
than opening the file and printing the lines of the file. I can do that
without the CSV module. I need to know how to address individual fields.

Something like this: (not actual python code)


import csv
reader = csv.reader(file("some.csv"))
for row in reader:
print row.field(3) or import csv
reader = csv.reader(file("some.csv"))
if reader.field(2,1) == "test.zip": print "test.zip exists"
Thanks,-Mark

How about:

import csv
reader = csv.reader(file("some.csv"))
for row in reader:
print row[3]

Nick
 
P

Paul Watson

Mark F said:
To say I'm a python newbie would be an understatement but I think it is a
language worth learning and would make a great replacement for perl in our
shop.

I'm trying to read a CSV file using the python csv module but I can't seem
to determine how to use the API, it seems to me that I can do nothing more
than opening the file and printing the lines of the file. I can do that
without the CSV module. I need to know how to address individual fields.

Something like this: (not actual python code)


import csv
reader = csv.reader(file("some.csv"))
for row in reader:
print row.field(3) or import csv
reader = csv.reader(file("some.csv"))
if reader.field(2,1) == "test.zip": print "test.zip exists"
Thanks,-Mark

The interactive mode of Python is a great way to experiment and learn. I
would agree that the documentation in 12.20 should show an explicit example
of accessing a single field.

C:\src\python\test\csv>type t.csv
now,is,the,time
for,all,good,men
to,come,to,the
aid,of,their,country

C:\src\python\test\csv>python
Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information..... print "the row is %s" % (row)
.... print " the second item is %s" % (row[1])
....
the row is ['now', 'is', 'the', 'time']
the second item is is
the row is ['for', 'all', 'good', 'men']
the second item is all
the row is ['to', 'come', 'to', 'the']
the second item is come
the row is ['aid', 'of', 'their', 'country']
the second item is of
r2 = csv.reader(file('t.csv'))
s = r2.next()
s ['now', 'is', 'the', 'time']
s[1]
'is'
 
S

Skip Montanaro

Paul> I would agree that the documentation in 12.20 should show an
Paul> explicit example of accessing a single field.

Done...

Skip
 
M

Mark F

Thanks,
I'm not only impressed with the language but with the community as well.

Good work
-Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top