试图将文件加载到Python中,但它声明它不存在,即使它确实存在

时间:2014-09-14 01:50:57

标签: python

我正在尝试将csv文件加载到Pandas中。我得到一个奇怪的错误,我以前从未遇到过该文件即使存在也不存在。该错误还在消息“ree.csv”

中将文件调用为其他名称
import pandas as pd
tree = pd.read_csv('C:\Users\Desktop\tree.csv')

IOError                                   
Traceback (most recent call last)
<ipython-input-63-aed3b96442f2> in <module>()
----> 1 tree = pd.read_csv('C:\Users\Desktop\tree.csv')

C:\Users\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\io\parsers.pyc in parser_f(filepath_or_buffer, sep, dialect, compression, doublequote, escapechar, quotechar, quoting, skipinitialspace, lineterminator, header, index_col, names, prefix, skiprows, skipfooter, skip_footer, na_values, na_fvalues, true_values, false_values, delimiter, converters, dtype, usecols, engine, delim_whitespace, as_recarray, na_filter, compact_ints, use_unsigned, low_memory, buffer_lines, warn_bad_lines, error_bad_lines, keep_default_na, thousands, comment, decimal, parse_dates, keep_date_col, dayfirst, date_parser, memory_map, nrows, iterator, chunksize, verbose, encoding, squeeze, mangle_dupe_cols, tupleize_cols)
    398             )
    399 
--> 400         return _read(filepath_or_buffer, kwds)
    401 
    402     parser_f.__name__ = name

C:\Users\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\io\parsers.pyc in _read(filepath_or_buffer, kwds)
    196 
    197     # Create the parser.
--> 198     parser = TextFileReader(filepath_or_buffer, **kwds)
    199 
    200     if nrows is not None:

C:\Users\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\io\parsers.pyc in __init__(self, f, engine, **kwds)
    477             self.options['has_index_names'] = kwds['has_index_names']
    478 
--> 479         self._make_engine(self.engine)
    480 
    481     def _get_options_with_defaults(self, engine):

C:\Users\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\io\parsers.pyc in _make_engine(self, engine)
    584     def _make_engine(self, engine='c'):
    585         if engine == 'c':
--> 586             self._engine = CParserWrapper(self.f, **self.options)
    587         else:
    588             if engine == 'python':

C:\Users\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\io\parsers.pyc in __init__(self, src, **kwds)
    955         kwds['allow_leading_cols'] = self.index_col is not False
    956 
--> 957         self._reader = _parser.TextReader(src, **kwds)
    958 
    959         # XXX

C:\Users\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\parser.pyd in       pandas.parser.TextReader.__cinit__ (pandas\parser.c:2987)()

C:\Users\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\parser.pyd in pandas.parser.TextReader._setup_parser_source (pandas\parser.c:5345)()

IOError: File C:\Users\Desktop  ree.csv does not exist

3 个答案:

答案 0 :(得分:5)

试试这个:

'C:/Users/Desktop/tree.csv'

或者

'C:\\Users\\Desktop\\tree.csv'

答案 1 :(得分:1)

问题在于如何阅读您的路径。

tree = pd.read_csv('C:\Users\Desktop\tree.csv')

从此,\t被视为tab character

您可以使用3种解决方案:

选项1 - 使用原始字符串:

tree = pd.read_csv(r'C:\Users\Desktop\tree.csv')
  

当存在'r'或'R'前缀时,后跟一个字符   反斜杠包含在字符串中而没有更改,所有   反斜杠留在字符串中。

选项2 - 使用双引号。这会逃离\,以便您通过正确的路径。

tree = pd.read_csv('C:\\Users\\Desktop\\tree.csv')

选项3 - 将\更改为/

tree = pd.read_csv('C:/Users/Desktop/tree.csv')

所有这三个都将提供可以使用的正确路径。

答案 2 :(得分:0)

你没有给Python你认为你的文件名,因为字符串中的反斜杠字符被解释为特殊的转义字符。

使用不将反斜杠视为转义字符的原始字符串:

r'C:\Users\Desktop\tree.csv'