When python print string, it may report UnicodeEncodeError: ‘gbk’ codec can’t encode character and the python will be terminated. In this tutorial, we will introduce you how to fix this error.
An example code below:
# -*- coding:utf-8 -*- import emoji s = 'Python is :cookie:' print(emoji.emojize(s))
When you print emoji, this error will occur.
How to fix this error?
Add code below in your program.
import io import sys sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')
Then this error is fixed.