Skip to content
Advertisement

how to read csv files with mbcs codec in Python on Linux?

I’m trying to read CSV files with Western Europe (windows) encoding

df = pd.read_csv(FileName,encoding='mbcs', usecols=[1],header=4)

This code works well on Windows but not on Linux 18.04. (Error: unknown encoding: mbcs) Indeed, in the codecs python documentation, we have the information:

mbcs is for Windows only: Encode the operand according to the ANSI codepage (CP_ACP).

is there another way/name to decode my files in python on Linux? (I have thousand of files so I can’t save as on Excel)

Advertisement

Answer

If your systems uses a Western Europe encoding on Windows, the mbcs encoding (the ANSI codepage) is cp1252. So you should use:

df = pd.read_csv(FileName,encoding='cp1252', usecols=[1],header=4)

on both system to have a compatible code base.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement