简体   繁体   中英

Problems with pd.read_csv

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.

Then I tried df = pd.read_csv('C:\\tutorial.csv') and I received the following error message:

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:

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!

尝试转义反斜杠:

df = pd.read_csv('C:\\Users\\test.csv')

try use two back-slash '\\' instead of '\\'. It might have take it as a escape sign.. ?

另一种选择是在路径之前添加r ,即df = pd.read_csv(r'C:\\Users\\test.csv')

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