خواندن فایل در python - هفت خط کد انجمن پرسش و پاسخ برنامه نویسی

خواندن فایل در python

+1 امتیاز

سلام میخوام توی پایتون یه فایل csv بخونم.کدم اینه

import csv

with open(data0.csv,newline='') as csvfile:
    spamreader = csv.reader(csvfile,delimiter='  ',quotechar='|')
    for row in spamreader:
        print(', '.join(row))
        

این خطا رو میده

NameError                                 Traceback (most recent call last)
<ipython-input-2-e031a03f50b0> in <module>()
----> 1 with open(data0.csv,newline='') as csvfile:
      2     spamreader = csv.reader(csvfile,delimiter='  ',quotechar='|')
      3     for row in spamreader:
      4         print(', '.join(row))
      5 

NameError: name 'data0' is not defined

 import csv

%load data0.csv
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
C:\Users\EHSAN\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py in find_user_code(self, target, raw, py_only, skip_encoding_cookie, search_ns)
   3181         try:                                              # User namespace
-> 3182             codeobj = eval(target, self.user_ns)
   3183         except Exception:

<string> in <module>()

NameError: name 'data0' is not defined

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-4-0777464601b1> in <module>()
----> 1 get_ipython().magic('load data0.csv')

C:\Users\EHSAN\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py in magic(self, arg_s)
   2156         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2157         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2158         return self.run_line_magic(magic_name, magic_arg_s)
   2159 
   2160     #-------------------------------------------------------------------------

C:\Users\EHSAN\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py in run_line_magic(self, magic_name, line)
   2077                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2078             with self.builtin_trap:
-> 2079                 result = fn(*args,**kwargs)
   2080             return result
   2081 

<decorator-gen-46> in load(self, arg_s)

C:\Users\EHSAN\Anaconda3\lib\site-packages\IPython\core\magic.py in <lambda>(f, *a, **k)
    186     # but it's overkill for just that one bit of state.
    187     def magic_deco(arg):
--> 188         call = lambda f, *a, **k: f(*a, **k)
    189 
    190         if callable(arg):

C:\Users\EHSAN\Anaconda3\lib\site-packages\IPython\core\magics\code.py in load(self, arg_s)
    348         search_ns = 'n' in opts
    349 
--> 350         contents = self.shell.find_user_code(args, search_ns=search_ns)
    351 
    352         if 's' in opts:

C:\Users\EHSAN\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py in find_user_code(self, target, raw, py_only, skip_encoding_cookie, search_ns)
   3183         except Exception:
   3184             raise ValueError(("'%s' was not found in history, as a file, url, "
-> 3185                                 "nor in the user namespace.") % target)
   3186 
   3187         if isinstance(codeobj, string_types):

ValueError: 'data0.csv' was not found in history, as a file, url, nor in the user namespace.

 

باید چی کار کنم؟

ممنون

سوال شده دی 6, 1395  بوسیله ی k-eq (امتیاز 10)   1 1 1
برای خواندن فایل csv از کتابخانه pandas استفاده کنید .

1 پاسخ

+1 امتیاز
کد زیر درست کار میکند 

مشکل پیش آمده به این دلیل بود که اسم فایل مورد نظر در ' ' یا " " قرار نداشت 

 

 

import csv
 
with open('data0.csv',newline='') as csvfile:
    spamreader = csv.reader(csvfile,delimiter='  ',quotechar='|')
    for row in spamreader:
        print(', '.join(row))

 

پاسخ داده شده فروردین 5, 1396 بوسیله ی Fire360Boy (امتیاز 2,524)   6 24 43
...