zoukankan      html  css  js  c++  java
  • 使用Pandas.read_csv时出现OSError: Initializing from file failed

      在用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)

      亲测,均可用。

  • 相关阅读:
    Hadoop_HDFS文件读写代码流程解析和副本存放机制
    Hadoop_MapReduce流程
    Hadoop_YARN框架
    Spark任务流程笔记
    3D俄罗斯方块设计
    Hadoop_FileInputFormat分片
    二叉查找树的懒惰删除(lazy deletion)
    数组的三种随机排序方法
    SpringBoot @Async 异步处理业务逻辑和发短信逻辑
    json字符串转java对象
  • 原文地址:https://www.cnblogs.com/djw12333/p/13440636.html
Copyright © 2011-2022 走看看