Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 在Python中打印Unicode字符(使用';\x00';)_Python 2.7_Unicode - Fatal编程技术网

Python 2.7 在Python中打印Unicode字符(使用';\x00';)

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打印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在打印之前将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..

完美的就这样!谢谢