【问题标题】:Why do I get this OSError?为什么我会收到此 OSError?
【发布时间】:2018-08-24 16:06:43
【问题描述】:

我正在尝试使用 pandas read_csv 函数读取 CSV 文件,但我不断收到 OSError。我尝试了许多不同的修复方法,但似乎没有一个有效。我已经修复了以前的文件未找到错误,但意外收到此错误。这是我使用的代码。请注意,我使用 Jupyter Notebook 作为 Anaconda 的一部分。

import pandas as pd
import os

curDir = os.getcwd()
#print(curDir)

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

这是我得到的输出/错误:

OSError                                   Traceback (most recent call last)
<ipython-input-2-12283d886215> in <module>()
      6 
      7 melbourne_file_path = '..\Downloads\melb_data.csv'
----> 8 melbourne_data = pd.read_csv(melbourne_file_path)
      9 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

【问题讨论】:

  • 这里不是问题,而是一个旁注:对于 Windows 路径,始终使用原始字符串(或正斜杠分隔的字符串)。在这种情况下,'..\Downloads\melb_data.csv'r'..\Downloads\melb_data.csv' 相同,但如果文件或文件夹名称以 abftnr 开头,则有时uUx,可能还有一些我忘记了,省略 r 前缀使其成为原始字符串会默默地产生无效路径,因为反斜杠被解释为转义。
  • 在回溯之后是否应该有另一行(至少)提供有关OSError 的更多信息?
  • 抱歉回复晚了,但最后写的是OSError: Initializing from file failed

标签: python pandas csv jupyter-notebook


【解决方案1】:

如果使用 Spyder 3.7,请确保 #!/usr/bin/env python3 出现在第 1 行。有时,在未激活正确的 Anaconda 环境时会创建 temp.py 文件。 temp.py 文件可能未包含 #!/usr/bin/env python3。以下两张图片显示了两个文件。第一个缺少第 1 行的内容,第二个包含修复。

without fix

with fix

1) 确保使用 source activate 激活conda 环境

2) 确保 #!/usr/bin/env python3 出现在新 python 文件的第 1 行

【讨论】:

    【解决方案2】:

    试试:

    melbourne_file_path = '../Downloads/melb_data.csv'
    

    melbourne_file_path = '..\\Downloads\\melb_data.csv'
    

    【讨论】:

    • 抱歉回复晚了。我已经尝试过了,但仍然得到上面和最后的错误OSError: Initializing from file failed
    • @HD 请先尝试更新您的公寓套餐版本。康达更新--全部
    • 只是为了确认,我在 Anaconda Prompt 中输入?
    猜你喜欢
    • 1970-01-01
    • 2019-05-14
    • 2020-02-24
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多