Daniel Corona
Daniel Corona

Reputation: 1222

UnicodeDecodeError: cp932 codec can't decode byte 0x81 in position 81

I'm trying to open an '.obj' file using 2 python scripts, but when I try compiling the project, it throws me the next error message:

UnicodeDecodeError: cp932' codec can't decode byte 0x81 in position 81: illegal multibyte sequence 

My code looks like this:

CarString = 'volks.obj'
global car
global objects

obj = test3.object()    
car = obj.load_obj(CarString)

objects.append(glGenLists(1))

Class object:

class object():
    def __init__(self, obj = None):
        if obj:
            self.load_obj(obj)
            #self.displaylist = self.crear_dl()

    def load_obj(self, file):
        with open(file, 'r') as obj:
            data = obj.read() 

The

data = obj.read()

part is what throws me this error. I'm new to Python so I can use some help to fix this. Thanks.

Upvotes: 3

Views: 5802

Answers (1)

Billy
Billy

Reputation: 5609

Your volks.obj file is likely binary data, not text. The default data type in the open command is text, so you need to specify binary.

Try:

def load_obj(self, file):
    with open(file, 'rb') as obj:
        data = obj.read()

If the file does indeed contain text and is not the default encoding of your system (usually utf-8, but looking at your error message it's probably cp932), you must specify the text encoding in the open call.

with open(file, 'r', encoding=<encoding_type>) as obj:

Upvotes: 6

Related Questions