pandas.read_csv()エラーOSError:ファイルからの初期化に失敗しました



Pandas Read_csv Error Oserror



次のコードを実行します。

import os import pandas as pd import requests PATH = 'F: / Machine Learning / Python-Machine-Learning-Blueprints (python Machine Learning Practice Guidelines) / Chapter 01 / iris /' r=requests.get('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data') with open(PATH+'iris.data','w') as f: f.write(r.text) df=pd.read_csv(PATH+'iris.data', names=['sepal length', 'sepal width', 'petal length', 'petal width', 'class'])

与えられた結果:



--------------------------------------------------------------------------- OSError Traceback (most recent call last) in () ----> 1 df=pd.read_csv(PATH+'iris.data', names=['sepal length', 'sepal width', 'petal length', 'petal width', 'class']) c:usersadministratorappdatalocalprogramspythonpython36libsite-packagespandasioparsers.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 c:usersadministratorappdatalocalprogramspythonpython36libsite-packagespandasioparsers.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: c:usersadministratorappdatalocalprogramspythonpython36libsite-packagespandasioparsers.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): c:usersadministratorappdatalocalprogramspythonpython36libsite-packagespandasioparsers.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': c:usersadministratorappdatalocalprogramspythonpython36libsite-packagespandasioparsers.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\_libsparsers.pyx in pandas._libs.parsers.TextReader.__cinit__() pandas\_libsparsers.pyx in pandas._libs.parsers.TextReader._setup_parser_source() OSError: Initializing from file failed

パス、ファイル名はそのままです。エラー1048で、Cエンジンのデフォルトを使用してread_csv pandas()メソッドを呼び出すことがわかった場合、ファイルパスに中国名が含まれていると、Cエンジンを使用すると文句が表示されます。エンジンはPythonに変更され、この問題を解決できます。

解決策:エンジンがPythonであることを指定します



df=pd.read_csv(path, names=['sepal length', 'sepal width', 'petal length', 'petal width', 'class'], engine = 'python') # engine as specified Python