2

I'm trying to open a file with MBCS encoding in Python but I'm getting an error.

If I write:

fileIN = open(filename, "r", encoding = "mbcs")

I get:

Traceback (most recent call last):
 File "/data/stru0039/Rotation/test.py", line 144, in <module>
   fileIN = open(filename, "r", encoding = "mbcs")
LookupError: unknown encoding: mbcs

Is there a way to install this codec? Thanks!

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Felipe Moser
  • 323
  • 4
  • 19
  • 2
    MBCS is not an encoding, it's a category of encodings, namely those that use a variable number of bytes per character (or a fixed number, usually two). So you need to find out which one your file is using (UTF-8 is the most common one) and use that. – Tim Pietzcker Jul 09 '18 at 12:54
  • 2
    Thanks for the correction! I sorted it now, it was ISO-8859-1 :) – Felipe Moser Jul 09 '18 at 13:35

2 Answers2

2

The mbcs encoding is only available on Windows. It refers to whatever the current Windows "ANSI" code page (CP_ACP) is.¹

If you are not running it on Windows, just guess an encoding and use it. If you are running it on Windows but through something like MSYS2 or Cygwin, you get a bug to report since someone is failing to detect the underlying Windows API.


¹ For those reading this answer, it's probably not an MBCS, but just a single-byte encoding like CP1252 (most similar to ISO-8859-1).

Mingye Wang
  • 1,107
  • 9
  • 32
0

In Ubuntu you can either convert the file to UTF-8

filename.encode('mbcs').decode('utf-8')

or change the format with an editor.

Ophelia
  • 1
  • 1