简体   繁体   English

当我尝试在jupyter笔记本中加载CSV文件时,为什么会出现nameerror

[英]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.

相关问题 当我尝试导入模块时在Jupyter Notebook NameError中 - In Jupyter Notebook NameError when I try to import a module 当我将csv文件加载到jupyter笔记本中时(使用python和pandas),为什么列未对齐? - Why are the columns misaligned when I load a csv file into jupyter notebook (using python and pandas)? 如何在 Jupyter Notebook 中阅读此 CSV? - How do I read this CSV in Jupyter Notebook? 为什么在通过 conda 启动 jupyter notebook 时出现 python37.dll 错误 - Why do I get a python37.dll error when starting jupyter notebook via conda 我已经在Windows中安装了TensorFlow,但是当我尝试将其导入Jupyter Notebook时出现错误 - I have installed TensorFlow in my Windows but I get an Error when I try to import it in my Jupyter Notebook 尝试使用matplotlib内联进行绘图时,为什么在jupyter笔记本中出现NonGuiException? - Why do I get NonGuiException in jupyter notebook when trying to plot using matplotlib inline? 当我尝试从 s3 加载数据时,sagemaker kernel 中的 Jupyter 笔记本一直死机 - Jupyter notebook in sagemaker kernel keeps dying when I try to load data from s3 如何将 csv 文件格式加载到 jupyter notebook? - How do you load a csv file format to jupyter notebook? 当我尝试在 Jupyter Notebook 上计算归一化时出错(使用“-”和 df) - Error when I try to calculate the normalization on Jupyter notebook (with '-' and df) 当我尝试引用以前在Python中定义的文件变量时,为什么会出现NameError? - Why am I getting a NameError when I try to reference to a file variable I have previously defined in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM