Fix Python UnicodeEncodeError: ‘gbk’ codec can’t encode character ‘\U0001f44d’ – Python Tutorial

By | July 14, 2019

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.

UnicodeEncodeError gbk 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.

Leave a Reply

Your email address will not be published. Required fields are marked *