Python pandas.read_ Oserror: initializing from file failed

Python version: Python 3.6
version pandas.read_ Oserror: initializing from file failed is usually caused by two cases: one is that the function parameter is path instead of file name, the other is that the function parameter is in Chinese.

# -*- coding: utf-8 -*-
"""
Created on Mon Jun  4 09:44:36 2018
@author: wfxu
"""
import pandas as pd
da1=pd.read_csv('F:\\datas')
da2=pd.read_csv('F:\\2.0 datas\\lists.csv')

In both cases, the error message is the same:

Traceback (most recent call last):
	(Error reporting details are not shown)
  File "pandas/_libs/parsers.pyx", line 720, in pandas._libs.parsers.TextReader._setup_parser_source

OSError: Initializing from file failed

For the first case, it’s very simple. The reason is that you don’t put the file name after the path. Just add the file name after the path. You can also switch the folder to the folder where the target file is located in the code. The process is too complicated, I don’t like it or recommend it, so I won’t show it
in the second case, even if the path and file name are complete, an error is still reported because there is Chinese in this parameter, but doesn’t Python 3 already support Chinese?Referring to the cause of the error and the source code of panda, it is found that the read of panda is called_ When using the CSV () method, C engine is used as the parser engine by default. When the file name contains Chinese, using C engine will make mistakes in some cases. So I’m calling read_ The problem can be solved by specifying engine as Python when using the CSV () method.

da4=pd.read_csv('F:\\datas\\lists.csv',engine='python')

For the second case, another solution is to use the open function to open the file, and then access the data in it

da3=pd.read_csv(open('F:\\4.0 \\2.0 datas\\02.lists.csv'))

Well, the reasons for this error are understood, and the solution is very simple and crude, isn’t it very simple!

Similar Posts: