pandas: oserror with accent/special character in file path and file name

前端 未结 3 509
春和景丽
春和景丽 2020-12-22 05:25

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. A

3条回答
  •  轮回少年
    2020-12-22 06:14

    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 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

    I get two solutions around this:

    1.- Use this code to change all the app to works with the prior Python <= 3.5 encoding ("mbcs")

    import sys
    sys._enablelegacywindowsfsencoding()
    

    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")
    

    You can see this post: pandas.read_csv can't import file with accent mark in path

提交回复
热议问题