The Python 3.4 and Python 3.8/3.9 are different when I try execute below statement:
print('u212B')
Python 3.8/3.9 can print it correctly.
?
Python 3.4 will report an exception:
Traceback (most recent call last):
File "test.py", line 9, in <module>
print('u212B')
UnicodeEncodeError: 'gbk' codec can't encode character 'u212b' in position 0: illegal multibyte sequence
And according to this page, I can avoid the exception by overwrite sys.stdout via statement:
sys.stdout = io.TextIOWrapper(buffer=sys.stdout.buffer,encoding='utf-8')
But python 3.4 still print different charactor as below:
鈩?
So my questions are:
- Why do different python versions have different behaviors on stand output print?
- How can I print correct value
?
in python 3.4?
Edit 1:
I guess the difference is caused by PEP 528 -- Change Windows console encoding to UTF-8. But I still don't understand the machanism of console encoding and how I can print correct character in Python 3.4.
Edit 2:
One more difference, sys.getfilesystemencoding()
will get utf-8
in Python 3.8/3.9 and get mbcs
in Python 3.4.
question from:
https://stackoverflow.com/questions/65839911/why-do-different-python-versions-have-different-behaviors-on-stand-output-print 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…