Python 2.7 在Python中打印Unicode字符(使用';\x00';)
我正在尝试用python打印unicode字符 正在发生的事情:Python 2.7 在Python中打印Unicode字符(使用';\x00';),python-2.7,unicode,Python 2.7,Unicode,我正在尝试用python打印unicode字符 正在发生的事情: $ python -c "print u'TEXT'" | xxd 0000000: 5445 5854 0a TEXT. 预期: $ python -c "print u'TEXT'" | xxd 0000000: 5400 4500 5800 5400 0a T.E.X.T.. 我做错了什么?请帮忙 Python在打印之前将Unic
$ python -c "print u'TEXT'" | xxd
0000000: 5445 5854 0a TEXT.
预期:
$ python -c "print u'TEXT'" | xxd
0000000: 5400 4500 5800 5400 0a T.E.X.T..
我做错了什么?请帮忙 Python在打印之前将Unicode字符串转换为字节。您看到的是正确的输出,例如,
b'T'==b'\x54'
:
$ python -c"print u'TEXT'.encode('ascii')" | xxd
0000000: 5445 5854 0a TEXT.
不要混淆Unicode字符串和UTF-16字符编码的bytestring:
$ python -c"print u'TEXT'.encode('utf-16le')" | xxd
0000000: 5400 4500 5800 5400 0a T.E.X.T..
您可以使用pythonionecoding
环境变量更改用于对整个脚本的输出进行编码的字符编码:
$ PYTHONIOENCODING=utf-16le python -c"print u'TEXT'" | xxd
0000000: 5400 4500 5800 5400 0a T.E.X.T..
完美的就这样!谢谢