在用pandas.read_csv()函数直接读取批量的包含中文名的CSV文件时,发生如下错误:
File "D:iancheng_languagePythonlibsite-packagespandasioparsers.py", line 678, in parser_f
return _read(filepath_or_buffer, kwds)
File "D:iancheng_languagePythonlibsite-packagespandasioparsers.py", line 440, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "D:iancheng_languagePythonlibsite-packagespandasioparsers.py", line 787, in __init__
self._make_engine(self.engine)
File "D:iancheng_languagePythonlibsite-packagespandasioparsers.py", line 1014, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "D:iancheng_languagePythonlibsite-packagespandasioparsers.py", line 1708, in __init__
self._reader = parsers.TextReader(src, **kwds)
File "pandas\_libsparsers.pyx", line 384, in pandas._libs.parsers.TextReader.__cinit__
File "pandas\_libsparsers.pyx", line 697, in pandas._libs.parsers.TextReader._setup_parser_source
OSError: Initializing from file failed
问题原因:
文件名里包含中文名;郑州胎记去除哪家好 http://m.zykdbh.com/
传入参数是文件路径而非文件名
解决方案:
方案一:可在read_csv之前先用open()函数
f = open(analysis_output_path +'metrics.csv')
print('用read_csv读取的csv文件:', pd.read_csv(f))
方案二:不加open()函数,在read_csv里面设置engine参数。【engine=‘python’】
df=pd.read_csv(analysis_output_path +'metrics.csv',engine='python')
print('用read_csv读取的csv文件:', df)
亲测,均可用。