[英]why do i get a nameerror when i try to load a CSV-File in jupyter notebook
my csv file has the following structure: 我的csv文件具有以下结构:
ID fromEmail ID toEmail
134 a@a.com 23 b@b.com
33 aa@a.com 323 bbb@b.com
i have the following code on my jupyter notebook: 我在jupyter笔记本上有以下代码:
import csv as pt
with open(dnc-temporalGraph.csv, 'rb') as f:
data = list(csv.reader(f))
and the following NameError: 和以下NameError:
NameError Traceback (most recent call last)
<ipython-input-65-1b0399e4e4b5> in <module>()
1 import csv as pt
----> 2 with open(dnc-temporalGraph.csv, 'rb') as f:
3 data = list(csv.reader(f))
NameError: name 'dnc' is not defined
i've checked out some other questions like this and this but still couldn't figure out what im doing wrong here. 我已经签出像其他一些问题, 这个和这个 ,但还是无法弄清楚什么即时做错了什么。 if i put the filename between single quotation marks as in the second link, then i get an IOError 如果我将文件名放在第二个链接中的单引号之间,那么我将收到IOError
IOError: [Errno 2] No such file or directory: 'dnc-temporalGraph.csv'
help please? 请帮助?
You need to pass the file name as a string: 您需要将文件名作为字符串传递:
with open("dnc-temporalGraph.csv", 'rb') as f:
Otherwise it will think you are calling a variable dnc
否则会认为您正在调用变量dnc
Make sure the notebook is in the same location as your csv
. 确保笔记本计算机与csv
放在同一位置。 If it's not in the same location you have to give the full path: "C:/user/x/file_name.csv"
如果不在同一位置,则必须提供完整路径: "C:/user/x/file_name.csv"
Lastly you are importing csv as pt
最后,您将csv导入为pt
Either get rid of as pt
or change the entry "csv.reader(f)"
to "pt.reader(f)"
删除as pt
或将条目"csv.reader(f)"
更改为"pt.reader(f)"
So your code should be: 因此,您的代码应为:
import csv as pt
with open("dnc-temporalGraph.csv", 'rb') as f:
data = list(pt.reader(f))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.