I have Anaconda 3 on Windows 10. I am using pd.read_csv() to load csv files but I get error messages. To begin with I tried df = pd.read_csv('C:\direct_marketing.csv')
which worked and the file was imported.
我在Windows 10上安装了Anaconda 3。我正在使用pd.read_csv()加载CSV文件,但收到错误消息。首先,我尝试了df=pd.read_csv(‘C:\Direct_market ing.csv’),它起作用了,并且文件被导入。
Then I tried df = pd.read_csv('C:\tutorial.csv')
and I received the following error message:
然后我尝试df=pd.read_csv(‘C:\tutorial.csv’),收到以下错误消息:
Traceback (most recent call last):
File "<ipython-input-3-ce208cc2684f>", line 1, in <module>
df = pd.read_csv('C:\tutorial.csv')
File "C:\Users\Alexandros_7\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 562, in parser_f
return _read(filepath_or_buffer, kwds)
File "C:\Users\Alexandros_7\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 315, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "C:\Users\Alexandros_7\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 645, in __init__
self._make_engine(self.engine)
File "C:\Users\Alexandros_7\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 799, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "C:\Users\Alexandros_7\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 1213, in __init__
self._reader = _parser.TextReader(src, **kwds)
File "pandas\parser.pyx", line 358, in pandas.parser.TextReader.__cinit__ (pandas\parser.c:3427)
File "pandas\parser.pyx", line 628, in pandas.parser.TextReader._setup_parser_source (pandas\parser.c:6861)
OSError: File b'C:\tutorial.csv' does not exist
Then I moved the file to a new folder and renamed it and again used read.csv() to import it:
然后,我将文件移到一个新文件夹中,重命名它,并再次使用read.csv()将其导入:
df = pd.read_csv('C:\Users\test.csv')
This time I received a different error message:
这一次,我收到了不同的错误消息:
File "<ipython-input-5-03c6d380c174>", line 1
df = pd.read_csv('C:\Users\test.csv')
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
Could you help me understand what is going on and how to handle this situation?
你能帮我了解一下发生了什么以及如何处理这种情况吗?
Thanks a lot!
非常感谢!
更多回答
优秀答案推荐
Try escaping the backslashes:
尝试转义反斜杠:
df = pd.read_csv('C:\\Users\\test.csv')
try use two back-slash '\' instead of '\'. It might have take it as a escape sign.. ?
试着用两个反斜杠‘\’代替‘\’。它可能把它当成了逃生的信号..?
Another option is to add r
before the path i.e. df = pd.read_csv(r'C:\Users\test.csv')
另一种选择是在路径前添加r,即df=pd.read_csv(r‘C:\USERS\Test.csv’)
I had the same mistake and put
我犯了同样的错误,把
df = pd.read_csv('test.csv')
instead of
而不是
df = pd.read_csv('C:\Users\test.csv')
I saved the file in the same directory to anaconda
我将同一目录中的文件保存到了Anaconda
更多回答
我是一名优秀的程序员,十分优秀!