I am trying to use str.encode()
but I get
我正在尝试使用str.encode(),但我得到了。
>>> "hello".encode(hex)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be string, not builtin_function_or_method
I have tried a bunch of variations and they seem to all work in Python 2.5.2, so what do I need to do to get them to work in Python 3.1?
我已经尝试了一些变体,它们似乎都在Python 2.5.2中工作,那么我需要做什么才能让它们在Python 3.1中工作呢?
8 个解决方案
#1
62
The hex
codec has been chucked in 3.x. Use binascii
instead:
十六进制编解码器已经被扔进了3。x。用binascii代替:
>>> binascii.hexlify(b'hello')
b'68656c6c6f'
#2
17
You've already got some good answers, but I thought you might be interested in a bit of the background too.
你已经得到了一些不错的答案,但我想你可能也会对一些背景感兴趣。
Firstly you're missing the quotes. It should be:
首先,你错过了引言。应该是:
"hello".encode("hex")
Secondly this codec hasn't been ported to Python 3.1. See here. It seems that they haven't yet decided whether or not these codecs should be included in Python 3 or implemented in a different way.
第二,这个编解码器没有被移植到Python 3.1中。在这里看到的。似乎他们还没有决定是否应该将这些编解码器包含在Python 3中,或者以不同的方式实现。
If you look at the diff file attached to that bug you can see the proposed method of implementing it:
如果你看一下附加到那个bug的diff文件,你可以看到它的实现方法:
import binascii
output = binascii.b2a_hex(input)
#3
15
binascii methodes are easier by the way
顺便说一下,binascii方法更简单。
>>> import binascii
>>> x=b'test'
>>> x=binascii.hexlify(x)
>>> x
b'74657374'
>>> y=str(x,'ascii')
>>> y
'74657374'
>>> x=binascii.unhexlify(x)
>>> x
b'test'
>>> y=str(x,'ascii')
>>> y
'test'
Hope it helps. :)
希望它可以帮助。:)
#4
6
In Python 3, encode the string to bytes and use the hex()
method, returning a string.
在Python 3中,将字符串编码为字节,并使用hex()方法返回字符串。
s = "hello".encode("utf-8").hex()
s
# '68656c6c6f'
Optionally convert the string back to bytes:
可选地将字符串转换为字节:
b = bytes(s, "utf-8")
b
# b'68656c6c6f'
#5
5
base64.b16encode
and base64.b16decode
convert bytes to and from hex and work across all Python versions. The codecs approach also works, but is less straightforward in Python 3.
base64。b16encode和base64。b16decode将字节转换为和从十六进制转换到所有Python版本。codecs方法也有效,但是在Python 3中没有那么简单。
#6
4
In Python 3, all strings are unicode. Usually, if you encode an unicode object to a string, you use .encode('TEXT_ENCODING')
, since hex
is not a text encoding, you should use codecs.encode()
to handle arbitrary codecs. For example:
在Python 3中,所有字符串都是unicode。通常,如果将unicode对象编码为字符串,则使用.encode('TEXT_ENCODING'),因为十六进制不是文本编码,您应该使用codecs.encode()来处理任意编解码器。例如:
>>>> "hello".encode('hex')
LookupError: 'hex' is not a text encoding; use codecs.encode() to handle arbitrary codecs
>>>> import codecs
>>>> codecs.encode(b"hello", 'hex')
b'68656c6c6f'
Again, since "hello" is unicode, you need to indicate it as a byte string before encoding to hexadecimal. This may be more inline with what your original approach of using the encode
method.
同样,因为“hello”是unicode,所以在编码到十六进制之前,需要将它表示为字节字符串。这可能与您使用编码方法的原始方法更加内联。
The differences between binascii.hexlify
and codecs.encode
are as follow:
binascii之间的区别。hexlify和编解码器。编码是:
-
binascii.hexlify
binascii.hexlify
Hexadecimal representation of binary data.
二进制数据的十六进制表示。
The return value is a bytes object.
返回值是一个字节对象。
Type: builtin_function_or_method
类型:builtin_function_or_method
-
codecs.encode
codecs.encode
encode(obj, [encoding[,errors]]) -> object
编码(obj,[编码[、错误]])- >对象
Encodes obj using the codec registered for encoding. encoding defaults to the default encoding. errors may be given to set a different error handling scheme. Default is 'strict' meaning that encoding errors raise a ValueError. Other possible values are 'ignore', 'replace' and 'xmlcharrefreplace' as well as any other name registered with codecs.register_error that can handle ValueErrors.
使用注册编码的编解码器编码obj。编码默认为默认编码。可以使用错误设置不同的错误处理方案。默认是“严格”的,这意味着编码错误会引发一个ValueError。其他可能的值是“ignore”、“replace”和“xmlcharfreplace”,以及在codecs中注册的任何其他名称。可以处理ValueErrors的register_error。
Type: builtin_function_or_method
类型:builtin_function_or_method
#7
3
Use hexlify - http://epydoc.sourceforge.net/stdlib/binascii-module.html
使用hexlify - http://epydoc.sourceforge.net/stdlib/binascii-module.html
#8
0
Yet another method:
另一个方法:
s = 'hello'
h = ''.join([hex(ord(i)) for i in s]);
# outputs: '0x680x650x6c0x6c0x6f'
This basically splits the string into chars, does the conversion through hex(ord(char))
, and joins the chars back together. In case you want the result without the prefix 0x
then do:
这基本上是将字符串分割成字符,通过十六进制进行转换(ord(char)),并将这些字符连接在一起。如果你想要的结果没有前缀0x那么做:
h = ''.join([str(hex(ord(i)))[2:4] for i in s]);
# outputs: '68656c6c6f'
Tested with Python 3.5.3.
与Python 3.5.3测试。
#1
62
The hex
codec has been chucked in 3.x. Use binascii
instead:
十六进制编解码器已经被扔进了3。x。用binascii代替:
>>> binascii.hexlify(b'hello')
b'68656c6c6f'
#2
17
You've already got some good answers, but I thought you might be interested in a bit of the background too.
你已经得到了一些不错的答案,但我想你可能也会对一些背景感兴趣。
Firstly you're missing the quotes. It should be:
首先,你错过了引言。应该是:
"hello".encode("hex")
Secondly this codec hasn't been ported to Python 3.1. See here. It seems that they haven't yet decided whether or not these codecs should be included in Python 3 or implemented in a different way.
第二,这个编解码器没有被移植到Python 3.1中。在这里看到的。似乎他们还没有决定是否应该将这些编解码器包含在Python 3中,或者以不同的方式实现。
If you look at the diff file attached to that bug you can see the proposed method of implementing it:
如果你看一下附加到那个bug的diff文件,你可以看到它的实现方法:
import binascii
output = binascii.b2a_hex(input)
#3
15
binascii methodes are easier by the way
顺便说一下,binascii方法更简单。
>>> import binascii
>>> x=b'test'
>>> x=binascii.hexlify(x)
>>> x
b'74657374'
>>> y=str(x,'ascii')
>>> y
'74657374'
>>> x=binascii.unhexlify(x)
>>> x
b'test'
>>> y=str(x,'ascii')
>>> y
'test'
Hope it helps. :)
希望它可以帮助。:)
#4
6
In Python 3, encode the string to bytes and use the hex()
method, returning a string.
在Python 3中,将字符串编码为字节,并使用hex()方法返回字符串。
s = "hello".encode("utf-8").hex()
s
# '68656c6c6f'
Optionally convert the string back to bytes:
可选地将字符串转换为字节:
b = bytes(s, "utf-8")
b
# b'68656c6c6f'
#5
5
base64.b16encode
and base64.b16decode
convert bytes to and from hex and work across all Python versions. The codecs approach also works, but is less straightforward in Python 3.
base64。b16encode和base64。b16decode将字节转换为和从十六进制转换到所有Python版本。codecs方法也有效,但是在Python 3中没有那么简单。
#6
4
In Python 3, all strings are unicode. Usually, if you encode an unicode object to a string, you use .encode('TEXT_ENCODING')
, since hex
is not a text encoding, you should use codecs.encode()
to handle arbitrary codecs. For example:
在Python 3中,所有字符串都是unicode。通常,如果将unicode对象编码为字符串,则使用.encode('TEXT_ENCODING'),因为十六进制不是文本编码,您应该使用codecs.encode()来处理任意编解码器。例如:
>>>> "hello".encode('hex')
LookupError: 'hex' is not a text encoding; use codecs.encode() to handle arbitrary codecs
>>>> import codecs
>>>> codecs.encode(b"hello", 'hex')
b'68656c6c6f'
Again, since "hello" is unicode, you need to indicate it as a byte string before encoding to hexadecimal. This may be more inline with what your original approach of using the encode
method.
同样,因为“hello”是unicode,所以在编码到十六进制之前,需要将它表示为字节字符串。这可能与您使用编码方法的原始方法更加内联。
The differences between binascii.hexlify
and codecs.encode
are as follow:
binascii之间的区别。hexlify和编解码器。编码是:
-
binascii.hexlify
binascii.hexlify
Hexadecimal representation of binary data.
二进制数据的十六进制表示。
The return value is a bytes object.
返回值是一个字节对象。
Type: builtin_function_or_method
类型:builtin_function_or_method
-
codecs.encode
codecs.encode
encode(obj, [encoding[,errors]]) -> object
编码(obj,[编码[、错误]])- >对象
Encodes obj using the codec registered for encoding. encoding defaults to the default encoding. errors may be given to set a different error handling scheme. Default is 'strict' meaning that encoding errors raise a ValueError. Other possible values are 'ignore', 'replace' and 'xmlcharrefreplace' as well as any other name registered with codecs.register_error that can handle ValueErrors.
使用注册编码的编解码器编码obj。编码默认为默认编码。可以使用错误设置不同的错误处理方案。默认是“严格”的,这意味着编码错误会引发一个ValueError。其他可能的值是“ignore”、“replace”和“xmlcharfreplace”,以及在codecs中注册的任何其他名称。可以处理ValueErrors的register_error。
Type: builtin_function_or_method
类型:builtin_function_or_method
#7
3
Use hexlify - http://epydoc.sourceforge.net/stdlib/binascii-module.html
使用hexlify - http://epydoc.sourceforge.net/stdlib/binascii-module.html
#8
0
Yet another method:
另一个方法:
s = 'hello'
h = ''.join([hex(ord(i)) for i in s]);
# outputs: '0x680x650x6c0x6c0x6f'
This basically splits the string into chars, does the conversion through hex(ord(char))
, and joins the chars back together. In case you want the result without the prefix 0x
then do:
这基本上是将字符串分割成字符,通过十六进制进行转换(ord(char)),并将这些字符连接在一起。如果你想要的结果没有前缀0x那么做:
h = ''.join([str(hex(ord(i)))[2:4] for i in s]);
# outputs: '68656c6c6f'
Tested with Python 3.5.3.
与Python 3.5.3测试。