我在Windows 10上安装了Anaconda 3.我使用pd.read_csv()来加载csv文件,但是我收到了错误消息。首先,我尝试使用了df = pd.read_csv('C:\direct_marketing.csv')
,并且导入了文件。
然后我尝试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
然后我将文件移动到一个新文件夹并重命名,然后再次使用read.csv()导入它:
df = pd.read_csv('C:\Users\test.csv')
这次我收到了一条不同的错误消息:
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
你能帮我理解发生了什么以及如何处理这种情况吗?
非常感谢!
答案 0 :(得分:2)
尝试转义反斜杠:
df = pd.read_csv('C:\\Users\\test.csv')
答案 1 :(得分:1)
尝试使用两个反斜杠'\'而不是'\'。它可能会把它作为逃避标志..?
答案 2 :(得分:0)
另一种选择是在路径r
df = pd.read_csv(r'C:\Users\test.csv')