csv reading fields

Discussion in 'Python' started by Mark F, Jul 8, 2004.

  1. Mark F

    Mark F Guest

    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




    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    -----== Over 100,000 Newsgroups - 19 Different Servers! =-----
     
    Mark F, Jul 8, 2004
    #1
    1. Advertising

  2. "Mark F" <> wrote in message
    news:40ed95dd$...
    > 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
     
    Nick Smallbone, Jul 8, 2004
    #2
    1. Advertising

  3. Mark F

    Mark F Guest

    Thanks,
    could have sworn I tried that.

    -Mark

    "Nick Smallbone" <> wrote in message
    news:40ed9b43$0$7804$...
    >
    > "Mark F" <> wrote in message
    > news:40ed95dd$...
    > > 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
    >
    >





    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    -----== Over 100,000 Newsgroups - 19 Different Servers! =-----
     
    Mark F, Jul 8, 2004
    #3
  4. Mark F

    Paul Watson Guest

    "Mark F" <> wrote in message
    news:40ed95dd$...
    > 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.
    >>> import csv
    >>> reader = csv.reader(file("t.csv"))
    >>> for row in reader:

    .... 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'
     
    Paul Watson, Jul 8, 2004
    #4
  5. Paul> I would agree that the documentation in 12.20 should show an
    Paul> explicit example of accessing a single field.

    Done...

    Skip
     
    Skip Montanaro, Jul 8, 2004
    #5
  6. Mark F

    Mark F Guest

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

    Good work
    -Mark

    "Skip Montanaro" <> wrote in message
    news:...
    >
    > Paul> I would agree that the documentation in 12.20 should show an
    > Paul> explicit example of accessing a single field.
    >
    > Done...
    >
    > Skip





    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    -----== Over 100,000 Newsgroups - 19 Different Servers! =-----
     
    Mark F, Jul 8, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michal Mikolajczyk
    Replies:
    0
    Views:
    696
    Michal Mikolajczyk
    Feb 13, 2004
  2. Skip Montanaro
    Replies:
    0
    Views:
    768
    Skip Montanaro
    Feb 13, 2004
  3. Tintin92
    Replies:
    1
    Views:
    1,813
    Andrew Thompson
    Feb 14, 2007
  4. sso
    Replies:
    20
    Views:
    2,779
    Martin Gregorie
    Apr 26, 2009
  5. AMT2K5
    Replies:
    1
    Views:
    216
    Eric Schwartz
    Nov 8, 2005
Loading...

Share This Page