Python模块'csv'没有属性'DictReader' [英] Python module 'csv' has no attribute 'DictReader'
问题描述
因此,我正在为数据科学课程使用Python在Coursera上.如果有人有兴趣,我已将其链接了.
So I am following a Python for Data Science Course on Coursera. I have linked this in case anyone is interested.
他们有一个Jupyter Notebook,但我更喜欢用Notepad ++("NPP")编写代码,并从头开始编写所有代码,因为它可以帮助我更好地学习.
They have a Jupyter Notebook to follow along, but I prefer to code in Notepad++ ("NPP") and write it all from scratch as it helps me learn better.
我将下面的代码原样复制并粘贴到NPP中
The following code I have literally copy and pasted into NPP
import csv
with open('mpg.csv') as csvfile:
mpg = list(csv.DictReader(csvfile))
print(mpg[:3])
但是当我尝试运行文件时,出现以下AttributeError
But when I try to run the file, I am getting the following AttributeError
AttributeError: module 'csv' has no attribute 'DictReader'
I am not entirely sure why as I have seen this is indeed a module according to python documentation, and obviously it works on the Jupyter Notebook on Coursera
我认为可能值得注意的是我保存了"mpg.csv"文件的位置.我已经将其保存在python文件所在的同一文件夹(目录?)中.但是很显然,如果该位置有问题,那不会抛出AttributeError吗?
Something that I thought may be of note is the location in which I saved the 'mpg.csv' file. I have saved it in the same folder (directory?) that the python file is in. But obviously if the location was a problem It wouldn't be throwing out an AttributeError would it?
预先感谢您的帮助.
推荐答案
您是否偶然将Python文件命名为csv.py?不要那样做! :)
Did you happen to name your Python file csv.py? Don't do that! :)
您应避免将Python文件命名为关键字或类.
You should avoid naming Python files as keywords or classes.
这篇关于Python模块'csv'没有属性'DictReader'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!