reading csv problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ajikoe@gmail.com

    #1

    reading csv problem

    Hello,

    I use csv to take information from file.
    import csv
    reader = csv.reader(open ('t.csv'))

    for row in reader:
    print row # it is perfectly OK

    ---------------------------------------------------------------------
    But If I use this code I have problem
    import csv
    reader = csv.reader(open ('t.csv'))

    for row in reader:
    print row # it is perfectly OK
    for row in reader:
    print row # it is not printed on the monitor???

    Why does only the first print row work here?

    TIA,
    ajikoe

  • Cameron Walsh

    #2
    Re: reading csv problem

    ajikoe@gmail.co m wrote:
    Hello,
    >
    I use csv to take information from file.
    import csv
    reader = csv.reader(open ('t.csv'))
    >
    for row in reader:
    print row # it is perfectly OK
    >
    ---------------------------------------------------------------------
    But If I use this code I have problem
    import csv
    reader = csv.reader(open ('t.csv'))
    >
    for row in reader:
    print row # it is perfectly OK
    for row in reader:
    print row # it is not printed on the monitor???
    >
    Why does only the first print row work here?
    >
    TIA,
    ajikoe
    >
    Because reader is an iterator and has no means of going back to its
    beginning.

    You could make a list out of it:
    lines = list(csv.reader (open('t.csv')) )
    for row in lines:
    print row
    for row in lines:
    print row

    Hope it helps,

    Cameron.

    Comment

    • ajikoe@gmail.com

      #3
      Re: reading csv problem

      Thank you.


      On Nov 15, 1:33 pm, Cameron Walsh <cameron.wa...@ gmail.comwrote:
      aji...@gmail.co m wrote:
      Hello,
      >
      I use csv to take information from file.
      import csv
      reader = csv.reader(open ('t.csv'))
      >
      for row in reader:
      print row # it is perfectly OK
      >
      ---------------------------------------------------------------------
      But If I use this code I have problem
      import csv
      reader = csv.reader(open ('t.csv'))
      >
      for row in reader:
      print row # it is perfectly OK
      for row in reader:
      print row # it is not printed on the monitor???
      >
      Why does only the first print row work here?
      >
      TIA,
      ajikoeBecause reader is an iterator and has no means of going back to its
      beginning.
      >
      You could make a list out of it:
      lines = list(csv.reader (open('t.csv')) )
      for row in lines:
      print row
      for row in lines:
      print row
      >
      Hope it helps,
      >
      Cameron.

      Comment

      Working...