问题描述
我正在尝试使用pandas.read_csv
从某些.csv文件中获取数据.只要文件名或文件路径中没有重音(例如ä,é,ü),此方法就可以正常工作.一旦使用诸如düm1.csv
之类的文件名,就会出现以下错误:OSError: Initializing from file failed
.我的代码是:
I am trying to use pandas.read_csv
to get data from some .csv files. This works fine as long as there is no accent (e.g. ä,é,ü) in the file name or file path. As soon as I use a file name such as düm1.csv
I get the following error: OSError: Initializing from file failed
. My code is:
dum1 = pd.read_csv(r"C:\Users\MyName\Desktop\dumm12\düm1.csv", sep = ";", decimal = ",", encoding = "utf-8")
我正在使用pandas 0.20.1和python 3.6.0.我发现这在以前的版本中是一个问题,但我认为它已解决.有想法该怎么解决这个吗?我也发现了这一点: https://github.com/pandas-dev/pandas/issues/15086
I am using pandas 0.20.1 and python 3.6.0. I have found that this has been an issue in previous versions but I thought it had been resolved. Any ideas on how to fix this? I also found this:https://github.com/pandas-dev/pandas/issues/15086
pd.show_versions()的输出:
output of pd.show_versions():
已安装的版本提交:无的Python:3.6.0.final.0python位:64操作系统:Windows操作系统版本:10机器:AMD64处理器:Intel64 Family 6 Model 78 Stepping 3,正版英特尔字节序:小LC_ALL:无朗:en地点:无.无
INSTALLED VERSIONScommit: Nonepython: 3.6.0.final.0python-bits: 64OS: WindowsOS-release: 10machine: AMD64processor: Intel64 Family 6 Model 78 Stepping 3, GenuineIntelbyteorder: littleLC_ALL: NoneLANG: enLOCALE: None.None
熊猫:0.20.1pytest的:3.0.5点:9.0.1setuptools:27.2.0Cython:无numpy的:1.11.3scipy:0.18.1xarray:无IPython:5.2.2狮身人面像:1.5.1麻痹:0.4.1dateutil的:2.6.0pytz:2016.10blosc:无瓶颈:1.2.0表格:3.2.2numexpr的:2.6.2羽毛:无matplotlib:2.0.0openpyxl:2.4.1xlrd:1.0.0xlwt:1.2.0xlsxwriter:0.9.6lxml:3.7.3bs4:4.5.3html5lib:0.999sqlalchemy:1.1.5pymysql:无psycopg2:无jinja2:2.9.5s3fs:无pandas_gbq:无pandas_datareader:无
pandas: 0.20.1pytest: 3.0.5pip: 9.0.1setuptools: 27.2.0Cython: Nonenumpy: 1.11.3scipy: 0.18.1xarray: NoneIPython: 5.2.2sphinx: 1.5.1patsy: 0.4.1dateutil: 2.6.0pytz: 2016.10blosc: Nonebottleneck: 1.2.0tables: 3.2.2numexpr: 2.6.2feather: Nonematplotlib: 2.0.0openpyxl: 2.4.1xlrd: 1.0.0xlwt: 1.2.0xlsxwriter: 0.9.6lxml: 3.7.3bs4: 4.5.3html5lib: 0.999sqlalchemy: 1.1.5pymysql: Nonepsycopg2: Nonejinja2: 2.9.5s3fs: Nonepandas_gbq: Nonepandas_datareader: None
推荐答案
我遇到了类似的问题.在Windows系统中,使用Python 3.6的pandas.read_csv似乎出现了问题.
I had a similar problem. It's look like the problem occurs with pandas.read_csv with Python 3.6 in a Windows system.
Python 3.6将Windows文件系统编码从"mbcs"更改为"UTF-8".参见 Python PEP 529 .您可以使用命令sys.getfilesystemencoding()
获取当前文件系统编码
Python 3.6 change Windows filesystem encoding from "mbcs" to "UTF-8". See Python PEP 529. You can use the command sys.getfilesystemencoding()
to get the current file system encoding
我有两种解决方法:
1.--使用此代码将所有应用更改为可与以前的Python< = 3.5编码("mbcs")一起使用
1.- Use this code to change all the app to works with the prior Python <= 3.5 encoding ("mbcs")
import sys
sys._enablelegacywindowsfsencoding()
2.-将文件指针传递给pandas.read_csv
2.- Pass a file pointer to the pandas.read_csv
with open("C:\Users\MyName\Desktop\dumm12\düm1.csv", 'r') as fp:
dum1 = pd.read_csv(fp, sep = ";", decimal = ",", encoding = "utf-8")
您可以看到此帖子: pandas .read_csv无法导入路径中带有重音符号的文件
这篇关于pandas:文件格式和文件名中带有重音/特殊字符的oserror的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!