she*_*lbc 3 python unicode python-3.x
我一直在使用 python 进行 ascii 到二进制的翻译,并一直遇到解析结果的问题。最终我想看看 Python 命令生成了什么。
0xc2输出中似乎插入了胭脂(例如):
$ python -c 'print("\x80")' | xxd
00000000: c280 0a ...
Run Code Online (Sandbox Code Playgroud)
实际上,无论在哪里使用此类字节都会发生这种情况:
$ python -c 'print("Test\x80Test2\x81")' | xxd
00000000: 5465 7374 c280 5465 7374 32c2 810a Test..Test2...
Run Code Online (Sandbox Code Playgroud)
凭直觉,我仔细研究了UTF-8,果然,U+0080编码为0xc2 0x80. 显然,Python 冒昧地假设\x80我实际上是指U+0080. 有没有办法改变这个默认行为或以其他方式明确规定我的意图是包含单字节0x80而不是 UTF 编码?
Python 3.6.2
Python 3 将字符插入到 astr中是正确的事情,它是字符串,而不是字节序列。
UTF8 是默认编码。如果您需要插入一个字节,则需要一种不同的编码,其中该字符表示为一个字节。
$ PYTHONIOENCODING=iso-8859-1 python3 -c 'print("\x80")' | xxd
00000000: 800a
Run Code Online (Sandbox Code Playgroud)
蟒蛇编码
如果在运行解释器之前设置它,它将覆盖用于 stdin/stdout/stderr 的编码,语法为 encodingname:errorhandler。encodingname 和 :errorhandler 部分都是可选的,与 str.encode() 具有相同的含义。
| 归档时间: |
|
| 查看次数: |
1127 次 |
| 最近记录: |