7

This is my code z = (priv.to_string().encode('hex')) and I got this error:

"AttributeError: 'bytes' object has no attribute 'encode'"

looks like I missed something to show "encode" after the code:

z = (priv.to_string().

2
  • 1
    What is priv's type? Try str(priv) rather than priv.to_string() Commented Apr 16, 2019 at 6:15
  • it is a string, i put hex() after "z = (priv.to_string(). and after .encode this is my code now and it's fine "z = (priv.to_string().hex().encode(hex()) but i have syntax error in my next code print, here is my next code "print("private key = "+z+" " + "address = 0x"+address)" Commented Apr 16, 2019 at 6:30

2 Answers 2

12

Two problems here:

  • you're using priv.to_string() (which isn't a built-in method) instead of str(priv)
  • 'hex' has been removed as an encoding in Python 3, so with str(priv).encode('hex') you'll get the following error: LookupError: 'hex' is not a text encoding; use codecs.encode()to handle arbitrary codecs

However, since Python 3.5, you can simply do:

priv.hex()

with priv being a byte string.

Example:

priv = b'test'
print(priv.hex())

Output:

74657374
Sign up to request clarification or add additional context in comments.

1 Comment

I modified my code: "z = str(priv.to_string().hex().encode())" and it works! thanks a lot!
2

on Python3 systems older than version 3.5, you can from binascii import hexlify and use hexlify(priv.to_string())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.