Fix UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xba in position 0 – Python Tutorial

By | October 11, 2023

When we are using python to read a file with utf-8 encoding, we may get this error: UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xba in position 0. In this tutorial, we will introduce you how to fix it.

For example:

    def get_lines(file):
        charset = "utf-8"
        with open(file, "r", encoding=charset) as f:
            lines = f.readlines()

Run this code, we may get this error:

UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xba in position 0: invalid start byte

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xba in position 0: invalid start byte

How to fix?

It is easy to fix this error. Use encoding = gb18030

For example:

    def get_lines(file):
        charset = "gb18030"
        with open(file, "r", encoding=charset) as f:
            lines = f.readlines()
        return lines

Run this code, we will find this error is fixed.

Moreover, if you use print() to get this error, you can fix as follows:

import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="gb18030")