UnicodeEncodeError: ‘gbk‘ codec can‘t encode character ‘\xa6‘ in position 8: illegal multibyte seque

python error

UnicodeEncodeError: 'gbk' codec can't encode character '\xa6' in position 8: illegal multibyte seque

This error is usually caused by some characters that the gbk encoding cannot handle. The solution is to change the encoding to the more general utf-8, that is, specify the encoding format as utf-8 in the program. For example, in Python, you can add the following code at the beginning of the file:

# -*- coding: utf-8 -*-

insert image description here
If such problems also occur in input and output, you can also use the encode and decode methods to convert the encoding format. For example, if you want to encode a string into utf-8 format, you can use the following code:

str_unicode = "你好"
str_utf8 = str_unicode.encode('utf-8')
print(str_utf8)
b'\xe4\xbd\xa0\xe5\xa5\xbd'

Note: Do not change the encoding method arbitrarily in the program to avoid other problems.

Guess you like

Origin blog.csdn.net/programmer589/article/details/130297381