Pandas读取文件时报错Initializing from file failed

2022年8月27日13:14:36

当在用Python的Pandas库进行读取csv文件,出现了以下错误时:

Traceback (most recent call last):
  File "D:/Pycharm/PythonSpider/HelloWorld.py", line 18, in <module>
    shangHai = pd.read_csv('链家上海租房信息.csv')
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\parsers.py", line 678, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\parsers.py", line 440, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\parsers.py", line 787, in __init__
    self._make_engine(self.engine)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\parsers.py", line 1014, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\parsers.py", line 1708, in __init__
    self._reader = parsers.TextReader(src, **kwds)
  File "pandas\_libs\parsers.pyx", line 384, in pandas._libs.parsers.TextReader.__cinit__
  File "pandas\_libs\parsers.pyx", line 697, in pandas._libs.parsers.TextReader._setup_parser_source
OSError: Initializing from file failed

应该就是因为你的文件名是中文而导致没有办法解析:

比如:

f = pd.read_csv('要打开的文件.csv')

只需要这样改就可以了:

f = open('要打开的文件.csv','r', encoding='UTF-8')
file = pd.read_csv(f)
print(file)
  • 作者:白糖炒栗子~
  • 原文链接:https://blog.csdn.net/weixin_41571493/article/details/82529175
    更新时间:2022年8月27日13:14:36 ,共 1318 字。