error in python 3.7 'bytes' object has no attribute 'encode'

0

will not understand why I get that error (see title) in python 3.7 this is what I'm doing

>>> from struct import pack as spack
>>> a1=spack("LLLL", 45129401,92367215,681285731,1710201)
>>> a1.encode('hex')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'bytes' object has no attribute 'encode'
>>>
    
asked by crt 24.10.2018 в 00:00
source

1 answer

0

We are talking about str.encode() , that is, a chain type object method , however struct.pack() returns an object of type bytes that clearly mentions the error, does not have an attribute encode . The problem may be that you are trying to use 2x code with the 3x version of Python. If you are looking to convert the generated bytes to a hexadecimal string, instead of doing a1.encode('hex') you can do the following:

import codecs
print(codecs.encode(a1, 'hex_codec'))
    
answered by 24.10.2018 в 05:31