0
import csv

with open('SalesJan2009.csv') as csvfile:
    readcsv = csv.reader(csvfile, delimiter =',')
    print(readcsv)

When I run this code I get the following error:

Traceback (most recent call last):
  File "D:/ANIKET/PRACTICE PROGRAMS/python projects/csv.py", line 1, in <module>
    import csv
  File "D:\ANIKET\PRACTICE PROGRAMS\python projects\csv.py", line 3, in <module>
    readcsv = csv.reader(csvfile, delimiter =',')
AttributeError: module 'csv' has no attribute 'reader'

What should I change?

martineau
  • 119,623
  • 25
  • 170
  • 301
Aniket Bote
  • 3,456
  • 3
  • 15
  • 33

2 Answers2

3

Your own script is called "csv.py" so it's trying to import iteself. Since you do not have the attribute "reader" inside your own script, it's not found.

Try renaming the script you are running and you should be fine.

Tim Woocker
  • 1,883
  • 1
  • 16
  • 29
  • i m getting this result:"<_csv.reader object at 0x000002219B5B8798>" – Aniket Bote Mar 28 '18 at 22:55
  • That is the representation of the reader object you created. Have a look at the docs for more info on how to use that object: https://docs.python.org/3/library/csv.html#csv.reader – Tim Woocker Mar 28 '18 at 22:57
1

You are importing your own file (csv.py). You need to rename your csv.py file and try again. This is duplicate of AttributeError: 'module' object has no attribute 'reader'

jugu
  • 98
  • 1
  • 5