【问题标题】:I am getting a file not found error when trying to read a csv file using pandas. I am sure I have used the correct path to the file尝试使用 pandas 读取 csv 文件时出现文件未找到错误。我确定我使用了正确的文件路径
【发布时间】:2018-08-17 01:59:53
【问题描述】:

我正在尝试使用 pandas read_csv 函数读取 CSV 文件,但我不断收到文件未找到错误。这是我使用的代码,虽然我说我确定我使用了正确的路径,但我仍然可能是错误的,如果是这种情况,请向我指出错误。请注意,我使用 Jupyter Notebook 作为 Anaconda 的一部分。

import pandas as pd

melbourne_file_path = 'Downloads\melb_data.csv'
melbourne_data = pd.read_csv(melbourne_file_path) 
print(melbourne_data.describe())

我得到的错误是

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-3-f31a03883307> in <module>()
  2 
  3 melbourne_file_path = 'Downloads\melb_data.csv'    
----> 4 melbourne_data = pd.read_csv(melbourne_file_path)
  5 print(melbourne_data.describe())

~\Anaconda3\lib\site-packages\pandas\io\parsers.py in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, escapechar, comment, encoding, dialect, tupleize_cols, error_bad_lines, warn_bad_lines, skipfooter, skip_footer, doublequote, delim_whitespace, as_recarray, compact_ints, use_unsigned, low_memory, buffer_lines, memory_map, float_precision)
    707                     skip_blank_lines=skip_blank_lines)
    708 
--> 709         return _read(filepath_or_buffer, kwds)
    710 
    711     parser_f.__name__ = name

~\Anaconda3\lib\site-packages\pandas\io\parsers.py in _read(filepath_or_buffer, kwds)
    447 
    448     # Create the parser.
--> 449     parser = TextFileReader(filepath_or_buffer, **kwds)
    450 
    451     if chunksize or iterator:

~\Anaconda3\lib\site-packages\pandas\io\parsers.py in __init__(self, f, engine, **kwds)
    816             self.options['has_index_names'] = kwds['has_index_names']
    817 
--> 818         self._make_engine(self.engine)
    819 
    820     def close(self):

~\Anaconda3\lib\site-packages\pandas\io\parsers.py in _make_engine(self, engine)
   1047     def _make_engine(self, engine='c'):
   1048         if engine == 'c':
-> 1049             self._engine = CParserWrapper(self.f, **self.options)
   1050         else:
   1051             if engine == 'python':

~\Anaconda3\lib\site-packages\pandas\io\parsers.py in __init__(self, src, **kwds)
   1693         kwds['allow_leading_cols'] = self.index_col is not False
   1694 
-> 1695         self._reader = parsers.TextReader(src, **kwds)
   1696 
   1697         # XXX

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader.__cinit__()

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._setup_parser_source()

FileNotFoundError: File b'Downloads\\melb_data.csv' does not exist

感谢任何帮助。

这里的要求是我收到的错误。

OSError                                   Traceback (most recent call last)
<ipython-input-1-d428ae69b6da> in <module>()
      2 
      3 melbourne_file_path = 'C:/Users/Username/Downloads'
----> 4 melbourne_data = pd.read_csv(melbourne_file_path)
      5 print(melbourne_data.describe())

~\Anaconda3\lib\site-packages\pandas\io\parsers.py in 
parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, 
usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, 
true_values, false_values, skipinitialspace, skiprows, nrows, na_values, 
keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, 
infer_datetime_format, keep_date_col, date_parser, dayfirst, iterator, 
chunksize, compression, thousands, decimal, lineterminator, quotechar, 
quoting, escapechar, comment, encoding, dialect, tupleize_cols, 
error_bad_lines, warn_bad_lines, skipfooter, skip_footer, doublequote, 
delim_whitespace, as_recarray, compact_ints, use_unsigned, low_memory, 
buffer_lines, memory_map, float_precision)
    707                     skip_blank_lines=skip_blank_lines)
    708 
--> 709         return _read(filepath_or_buffer, kwds)
    710 
    711     parser_f.__name__ = name

~\Anaconda3\lib\site-packages\pandas\io\parsers.py in 
_read(filepath_or_buffer, kwds)
    447 
    448     # Create the parser.
--> 449     parser = TextFileReader(filepath_or_buffer, **kwds)
    450  
    451     if chunksize or iterator:

~\Anaconda3\lib\site-packages\pandas\io\parsers.py in __init__(self, f, 
engine, **kwds)
    816             self.options['has_index_names'] = 
kwds['has_index_names']
    817 
--> 818         self._make_engine(self.engine)
    819 
    820     def close(self):

~\Anaconda3\lib\site-packages\pandas\io\parsers.py in _make_engine(self, 
engine)
   1047     def _make_engine(self, engine='c'):
   1048         if engine == 'c':
-> 1049             self._engine = CParserWrapper(self.f, **self.options)
   1050         else:
   1051             if engine == 'python':

~\Anaconda3\lib\site-packages\pandas\io\parsers.py in __init__(self, src, 
**kwds)
   1693         kwds['allow_leading_cols'] = self.index_col is not False
   1694 
-> 1695         self._reader = parsers.TextReader(src, **kwds)
   1696 
   1697         # XXX

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader.__cinit__()

pandas/_libs/parsers.pyx in 
pandas._libs.parsers.TextReader._setup_parser_source()

OSError: Initializing from file failed

【问题讨论】:

    标签: python pandas csv jupyter-notebook data-science


    【解决方案1】:

    我认为您的问题来自文件路径中的反斜杠。

    使用 python,您始终可以使用正斜杠(“/”),它将在 Windows 系统上进行翻译。

    'C:/mydir'

    或者您可以尝试以下方法:

    'C:\\mydir'

    不过,最好的解决方案可能是使用os 包来正确选择最适合您系统的方案

    【讨论】:

    • 我已经使用操作系统包找到我需要的文件路径并输入它,但我得到一个操作系统错误。我已将错误编辑到原始帖子中。
    【解决方案2】:

    尝试使用完整路径而不是相对路径。您正在引用'Downloads\melb_data.csv',但可能是您的当前目录不是您认为的那样,尤其是在从 Juypter 运行时。尝试类似~/Downloads/melb_data.csv

    【讨论】:

    • 我使用了您建议的格式,现在出现操作系统错误。你想让我显示完整的错误吗?
    • 您使用的是什么操作系统?下载文件夹的完整路径是什么?
    • Windows 10 是操作系统,完整路径是 C:\Users\Username\Downloads
    • 尝试执行“C:/Users/Username/Downloads”,看看是否可行 - 如果不发布错误消息
    猜你喜欢
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 1970-01-01
    相关资源
    最近更新 更多