Help me understand why Unicode only works sometimes with Python

前端 未结 5 1221
别跟我提以往
别跟我提以往 2020-12-14 22:11

Here\'s a little program:

#!/usr/bin/env python
# -*- encoding: utf-8 -*-

print(\'abcd kΩ ☠ °C √Hz µF ü ☃ ♥\')  
print(u\'abcd kΩ ☠ °C √Hz µF ü ☃ ♥\')
         


        
5条回答
  •  温柔的废话
    2020-12-14 23:11

    There are two possible reasons:

    • Encoding of Unicode by print. You cannot output raw Unicode, so print needs to figure out how to convert it to the byte stream expected by the console (it uses sys.stdout.encoding AFAIK), which brings us to
    • Console support. Python does not control your terminal, so if it spits out UTF-8 while your terminal expects something else, you'll get mangled output.

提交回复
热议问题