3

I am doing a python project.I opened a new csv files and its contents are

 A     |  B
  -------------
  1.  200 | 201   
  2.  200 | 202
  3.  200 | 201
  4.  200 | 203
  5.  201 | 201
  6.  201 | 202
  ...........

And what I am doing is...

def csvvalidation(readers):
    for row in readers:
        print row
def checkduplicationcsv(reader):
    datalist = []
    for row in reader:
        print row
        content = list(row[i] for i in range(0,3))
        datalist.append(content)     
with open("new.csv", "rb") as infile:
    reader = csv.reader(infile)
    first_row = next(reader, None)  # skip the headers
    checkduplicationcsv(reader)
    csvvalidation(reader)

The problem is I can print the values only one time.The csvvalidation() function reader is not working.How can I use the reader object multiple times.I can't print its row values.What can I do?Please give me a solution.And I am not aware of seek() (I think that its pointing to the same reader again).So I tried infile.seek(0) after the first function but no use.nothing happens

Thanks in advance.

bob marti
  • 1,523
  • 3
  • 11
  • 27

3 Answers3

7

The reader is wrapped around a file pointer, and when that is used up, it's used up. Don't use it multiple times, use it once and then work with the array of data you read:

with open("new.csv", "rb") as infile:
    reader = csv.reader(infile)
    first_row = next(reader, None)  # skip the headers
    data = list(reader)             # read everything else into a list of rows

checkduplicationcsv(data)
csvvalidation(data)

Yes, your two functions will work without modification (unless they were already broken), because a list, a file, and a csv reader are all "iterables" that can be iterated over. Ain't Python grand...

alexis
  • 48,685
  • 16
  • 101
  • 161
3

If you cannot read the whole file into memory you can create two readers by means of tee:

from itertools import tee
with open("new.csv", "rb") as infile:
    reader = csv.reader(infile)
    first_row = next(reader, None)  # skip the headers
    reader1, reader2 = tee(reader, 2)
    checkduplicationcsv(reader1)
    csvvalidation(reader2)
Paweł Kordowski
  • 2,688
  • 1
  • 14
  • 21
  • Note, you may lose some functionality with tee, for example: `'itertools.tee' object has no attribute 'line_num'` – webaholik Mar 06 '19 at 06:02
2

It works. You should check your code again :)

with open("new.csv", "rb") as infile:
    reader = csv.reader(infile)
    first_row = next(reader, None)  
    checkduplicationcsv(reader)
    infile.seek(0)        # <- Add infile.seek(0)
    csvvalidation(reader)
tell k
  • 605
  • 2
  • 7
  • 18
  • just Look into my question I tried this method but its not working.then I thought the problem that may be the way of opening the file.I am not use this method to open a file. file = open('new.csv', 'rb').So i thought may be this is the problem – bob marti Mar 20 '17 at 12:26
  • @bobmarti This works for me, too. Not sure why it's not working for you. – glibdud Mar 20 '17 at 12:27
  • @bobmarti Really? At least in my Python2.7 environment that code works well. Let's reduce the data in the CSV file and try check again. – tell k Mar 20 '17 at 12:28
  • @tellk I am also using python 2.7.And excel file is too large.And I reduced it into only 5 rows.but its not working – bob marti Mar 20 '17 at 12:31
  • @bobmarti Will you add the contents of the 5 rows(CSV) and the results of the execution to your question section? – tell k Mar 20 '17 at 12:36
  • @tellk as its said my question.But in that question I removed last row and the output as ['200','201']['200','202']['200','203']['201','201'] – bob marti Mar 20 '17 at 12:40
  • @bobmarti Sorry, I don't know anymore, Finally I'll share the code I wrote. thx. https://gist.github.com/tell-k/a74daaab8fa66366e375e20373f22c08 – tell k Mar 20 '17 at 13:03