我是靠谱客的博主 迷人发箍,这篇文章主要介绍解决pandas.read_csv()出现“OSError: Initializing from file failed”问题,现在分享给大家,希望可以做个参考。
在使用pandas.read_csv()读取txt文件时出现了如下异常:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41H:Anacondaanacondalibsite-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, doublequote, delim_whitespace, low_memory, memory_map, float_precision) 676 skip_blank_lines=skip_blank_lines) 677 --> 678 return _read(filepath_or_buffer, kwds) 679 680 parser_f.__name__ = name H:Anacondaanacondalibsite-packagespandasioparsers.py in _read(filepath_or_buffer, kwds) 438 439 # Create the parser. --> 440 parser = TextFileReader(filepath_or_buffer, **kwds) 441 442 if chunksize or iterator: H:Anacondaanacondalibsite-packagespandasioparsers.py in __init__(self, f, engine, **kwds) 785 self.options['has_index_names'] = kwds['has_index_names'] 786 --> 787 self._make_engine(self.engine) 788 789 def close(self): H:Anacondaanacondalibsite-packagespandasioparsers.py in _make_engine(self, engine) 1012 def _make_engine(self, engine='c'): 1013 if engine == 'c': -> 1014 self._engine = CParserWrapper(self.f, **self.options) 1015 else: 1016 if engine == 'python': H:Anacondaanacondalibsite-packagespandasioparsers.py in __init__(self, src, **kwds) 1706 kwds['usecols'] = self.usecols 1707 -> 1708 self._reader = parsers.TextReader(src, **kwds) 1709 1710 passed_names = self.names is None 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
百度了一下,有说因为pandas不支持读中文文件名的csv文件的,也有说是因为传入的参数是文件的路径而非文件名的,但是经过排查和试验,都没能解决这个问题。
看了一下错误原因和pandas的源码,发现调用pandas的read_csv()方法时,默认使用C engine作为parser engine,而当文件名中含有中文的时候,用C engine在部分情况下就会出错。
尝试了一下在调用read_csv()方法时指定engine为Python,问题解决。
即通过设关键字参数engine='python’就能解决。
例子:
pandas.read_csv(filename,engine=‘python’),python要加引号
最后
以上就是迷人发箍最近收集整理的关于解决pandas.read_csv()出现“OSError: Initializing from file failed”问题的全部内容,更多相关解决pandas.read_csv()出现“OSError:内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复