sam_dw
sam_dw

Reputation: 159

Python CSV lookup

Taking in to consideration two csv files;

Why the bellow code will only print the B.csv rows at the first iteration of A.csv?

with open('A.csv', 'rU') as a_data, open('B.csv', 'rU') as b_data:
        a_rows = csv.reader(a_data, delimiter=',', quotechar='"')
        b_rows = csv.reader(b_data, delimiter='\t')

        for a_row in a_rows:
            print a_row[0]

            for b_row in b_rows:
                print b_row[0]

Supposing that:

Here is the representation of what is printed:

1000
10001
10002
.
.
.
11398
11399
1001
1002
1003
.
.
.
1179

Upvotes: 0

Views: 165

Answers (1)

vijay
vijay

Reputation: 779

b_rows is the csv reader pointer and not a list. So this reads line by line until EOD of b_rows in the first iteration.

In the next successive a_row iterations, b_rows will be always pointed to empty.

Upvotes: 1

Related Questions