简体   繁体   中英

why do i get a nameerror when i try to load a CSV-File in jupyter notebook

my csv file has the following structure:

 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:

 import csv as pt
 with open(dnc-temporalGraph.csv, 'rb') as f:
    data = list(csv.reader(f))

and the following 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: [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

Make sure the notebook is in the same location as your csv . If it's not in the same location you have to give the full path: "C:/user/x/file_name.csv"


Lastly you are importing csv as pt

Either get rid of as pt or change the entry "csv.reader(f)" to "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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM