iMa*_*gur 17 hex encode python-3.x
是否从Python 3.3中排除了十六进制编解码器?当我写代码
>>> s="Hallo"
>>> s.encode('hex')
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
s.encode('hex')
LookupError: unknown encoding: hex
Run Code Online (Sandbox Code Playgroud)
那是什么意思?我知道binascii.hexlify()但仍然.encode()方法很好!有什么建议吗?
Len*_*bro 36
不,使用encode()hexlify并不好.
您使用hex编解码器的方式在Python 2中工作,因为您可以在Python 2中调用encode()8位字符串,即您可以对已经编码的内容进行编码.这没有意义.encode()用于将Unicode字符串编码为8位字符串,而不是将8位字符串编码为8位字符串.
在Python 3中,您不能再调用encode()8位字符串,因此hex编解码器变得毫无意义并被删除.
虽然理论上你可以有一个hex编解码器并像这样使用它:
>>> import codecs
>>> hexlify = codecs.getencoder('hex')
>>> hexlify(b'Blaah')[0]
b'426c616168'
Run Code Online (Sandbox Code Playgroud)
使用binascii更容易,更好:
>>> import binascii
>>> binascii.hexlify(b'Blaah')
b'426c616168'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
55341 次 |
| 最近记录: |