1

Couldn't get what wrong in the code, as csv module has a csv.reader() function as per the documentation. But I am still getting this error:

Traceback (most recent call last):
  File "test_csv.py", line 4, in <module>
    read = csv.reader(csv, delimiter = ',')
AttributeError: '_io.TextIOWrapper' object has no attribute 'reader'

My code:

import csv

with open('test_csv.csv') as csv:
    read = csv.reader(csv, delimiter = ',')
    for row in read:
        print(row)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Here_2_learn
  • 5,013
  • 15
  • 50
  • 68

1 Answers1

7

You re-bound the name csv in the as target:

with open('test_csv.csv') as csv:

This masks the module name, so csv.reader is resolved on the file object.

Use a different target:

with open('test_csv.csv') as csvfile:
    read = csv.reader(csvfile, delimiter = ',')
    for row in read:
        print(row)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks Martijn, changing the line into "with open('test_csv.csv') as csvfile" has worked. It has resulted in conflict and hence the error. – Here_2_learn Jul 22 '16 at 10:05