close

[Solved] UnicodeEncodeError: ‘charmap’ codec can’t encode – character maps to , print function

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error UnicodeEncodeError: ‘charmap’ codec can’t encode – character maps to , print function in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How UnicodeEncodeError: ‘charmap’ codec can’t encode – character maps to , print function Error Occurs?

Today I get the following error UnicodeEncodeError: ‘charmap’ codec can’t encode – character maps to , print function in python.

How To Solve UnicodeEncodeError: ‘charmap’ codec can’t encode – character maps to , print function Error ?

  1. How To Solve UnicodeEncodeError: 'charmap' codec can't encode – character maps to , print function Error ?

    To Solve UnicodeEncodeError: 'charmap' codec can't encode – character maps to , print function Error Based on Dirk Stöcker's answer, here's a neat wrapper function for Python 3's print function. Use it just like you would use print.

  2. UnicodeEncodeError: 'charmap' codec can't encode – character maps to , print function

    To Solve UnicodeEncodeError: 'charmap' codec can't encode – character maps to , print function Error Based on Dirk Stöcker's answer, here's a neat wrapper function for Python 3's print function. Use it just like you would use print.

Solution 1

If you are using Windows command line to print the data, you should use

chcp 65001

This worked for me!

Solution 2

Based on Dirk Stöcker’s answer, here’s a neat wrapper function for Python 3’s print function. Use it just like you would use print.

As an added bonus, compared to the other answers, this won’t print your text as a bytearray (‘b”content”‘), but as normal strings (‘content’), because of the last decode step.

def uprint(*objects, sep=' ', end='\n', file=sys.stdout):
    enc = file.encoding
    if enc == 'UTF-8':
        print(*objects, sep=sep, end=end, file=file)
    else:
        f = lambda obj: str(obj).encode(enc, errors='backslashreplace').decode(enc)
        print(*map(f, objects), sep=sep, end=end, file=file)

uprint('foo')
uprint(u'Antonín Dvořák')
uprint('foo', 'bar', u'Antonín Dvořák')

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read