Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

I need to change Python's encoding from cp1252 to UTF-8. Using python 3.7.1, Atom, and Atom script package for terminal.

I have read about https://www.python.org/dev/peps/pep-0540/ (a solution to this? I dont know how to implement or if useful) I cannot find a sound resolution.

Currently it cannot handle '\u2705' or others. When checking the Python file directory I found ...Python\Python37\lib\encodings\cp1252.py

#When I run 

import locale

import sys

print(sys.getdefaultencoding())

print(locale.getpreferredencoding())

#I get 

utf-8

cp1252

[Finished in 0.385s]

#Error for print('\u2705')

Traceback (most recent call last):

File "C:\Users\en4ijjp\Desktop\junk.py", line 7, in <module>

print('\u2705').decode('utf-8')

File "C:\Users\en4ijjp\AppData\Local\Programs\Python\Python37\lib\encodings\cp1252.py", line 19, in encode

return codecs.charmap_encode(input,self.errors,encoding_table)[0]

UnicodeEncodeError: 'charmap' codec can't encode character '\u2705' in 

position 0: character maps to <undefined>

[Finished in 0.379s]

I expect my terminal to handle the characters and display them when using print()

1 Answer

0 votes
by (25.1k points)

You need to change stdout and stderr stream and change it's encoding. Like this:

import sys

import io

sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding = 'utf-8')

sys.stderr = io.TextIOWrapper(sys.stderr.detach(), encoding = 'utf-8')

Browse Categories

...