pandas中出现错误的解决办法:OSError: Initializing from file failed

原因:pandas不支持读路径或文件名中包含中文的csv/txt文件的

解决办法:

  1. 先open(“文件”),在read_csv(f)或read_table(f)
f = open("D:\GoogleDownloads\临时文件\idfa_info.txt")
idfa1 = pd.read_table(f, sep='\t')
  1. 在read_csv()或read_table()函数中添加参数:engine=‘python’
idfa2 = pd.read_table("D:\GoogleDownloads\临时文件\idfa_info.txt", sep='\t', engine='python')

错误为:

OSError                                   Traceback (most recent call last)
<ipython-input-24-e5fecc5bee61> in <module>()
----> 1 idfa3 = pd.read_table("D:\GoogleDownloads\临时文件\idfa_info.txt", sep='\t')

D:\software\Anaconda\Anaconda3-5.2.0\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, 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

D:\software\Anaconda\Anaconda3-5.2.0\lib\site-packages\pandas\io\parsers.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:

D:\software\Anaconda\Anaconda3-5.2.0\lib\site-packages\pandas\io\parsers.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):

D:\software\Anaconda\Anaconda3-5.2.0\lib\site-packages\pandas\io\parsers.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':

D:\software\Anaconda\Anaconda3-5.2.0\lib\site-packages\pandas\io\parsers.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\_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,编程语言)