I need to change Python's encoding from Windows-1252 to UTF-8. I am using Python 3.7.1, Atom, and the Atom script package for terminal.
I have read about PEP 540 -- Add a new UTF-8 Mode (a solution to this? I don’t 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().
This is resolved when putting the following at the top of your Python script. I am able to print all characters without error.
import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding = 'utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.detach(), encoding = 'utf-8')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With