python - pandas.read_csv can't import file with accent mark in path -


i developing application python , qt gui. need import file dataframe. use qfiledialog.getopenfilename path , filename open pandas.read_csv method. works until path special characters "ó". pandas.read_csv doesn't work , crash app.

i try reproduce error in console , have following results:

in[2]: import pandas pd backend qt5agg interactive backend. turning interactive mode on.  in[3]: path1 = 'f:/software_proyects/python/proyectos/test_read_csv/flowdata.txt' in[4]: df1 = pd.read_csv(path1, delim_whitespace=true, dtype=object)  in[5]: path2 = 'f:/software_proyects/python/proyectos/test_read_csv_with_ó/flowdata.txt' in[6]: df2 = pd.read_csv(path2, delim_whitespace=true, dtype=object) traceback (most recent call last):   file "c:\program files (x86)\anaconda3\lib\site-packages\ipython\core\interactiveshell.py", line 2881, in run_code     exec(code_obj, self.user_global_ns, self.user_ns)   file "<ipython-input-6-feba8e024d43>", line 1, in <module>     df2 = pd.read_csv(path2, delim_whitespace=true, dtype=object)   file "c:\program files (x86)\anaconda3\lib\site-packages\pandas\io\parsers.py", line 646, in parser_f     return _read(filepath_or_buffer, kwds)   file "c:\program files (x86)\anaconda3\lib\site-packages\pandas\io\parsers.py", line 389, in _read     parser = textfilereader(filepath_or_buffer, **kwds)   file "c:\program files (x86)\anaconda3\lib\site-packages\pandas\io\parsers.py", line 730, in __init__     self._make_engine(self.engine)   file "c:\program files (x86)\anaconda3\lib\site-packages\pandas\io\parsers.py", line 923, in _make_engine     self._engine = cparserwrapper(self.f, **self.options)   file "c:\program files (x86)\anaconda3\lib\site-packages\pandas\io\parsers.py", line 1390, in __init__     self._reader = _parser.textreader(src, **kwds)   file "pandas\parser.pyx", line 373, in pandas.parser.textreader.__cinit__ (pandas\parser.c:4184)   file "pandas\parser.pyx", line 669, in pandas.parser.textreader._setup_parser_source (pandas\parser.c:8471) oserror: initializing file failed 

the output of show_versions() is:

in[7]: pd.show_versions()  installed versions ------------------ commit: none python: 3.6.0.final.0 python-bits: 32 os: windows os-release: 10 machine: amd64 processor: intel64 family 6 model 78 stepping 3, genuineintel byteorder: little lc_all: none lang: none locale: none.none  pandas: 0.19.2 nose: 1.3.7 pip: 9.0.1 setuptools: 27.2.0 cython: 0.25.2 numpy: 1.11.3 scipy: 0.18.1 statsmodels: 0.6.1 xarray: none ipython: 5.1.0 sphinx: 1.5.1 patsy: 0.4.1 dateutil: 2.6.0 pytz: 2016.10 blosc: none bottleneck: 1.2.0 tables: 3.2.2 numexpr: 2.6.1 matplotlib: 2.0.0 openpyxl: 2.4.1 xlrd: 1.0.0 xlwt: 1.2.0 xlsxwriter: 0.9.6 lxml: 3.7.2 bs4: 4.5.3 html5lib: none httplib2: none apiclient: none sqlalchemy: 1.1.5 pymysql: none psycopg2: none jinja2: 2.9.4 boto: 2.45.0 pandas_datareader: none 

as read in post encoding pandas.read_csv when file name has accents problem fixed in pandas 0.14.0.

any recommendation solve problem?

looking in deep, behavior comes in combination of python 3.6 , pandas.read_csv in windows systems.

python 3.6 change windows filesystem encoding "mbcs" "utf-8". see python pep 529. use sys.getfilesystemencoding() current file system encoding

i solutions around this:

1.- use code change app works prior python <= 3.5 encoding ("mbcs")

import sys sys._enablelegacywindowsfsencoding() 

2.- pass file pointer pandas.read_csv

with open(path2, 'r') fp:     df2 = pd.read_csv(fp, delim_whitespace=true, dtype=object) 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -