Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fix AttributeError: 'bytes' object has no attribute 'encode'?

Tags:

python

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().

like image 871
cristina parker Avatar asked Apr 16 '19 06:04

cristina parker


People also ask

What is “object has no attribute Python error”?

Attributes are functions or properties associated with an object of a class. Everything in Python is an object, and all these objects have a class with some attributes. We can access such properties using the . operator. This tutorial will discuss the object has no attribute python error in Python. This error belongs to the AttributeError type.

What is attributeerror in Python?

This error belongs to the AttributeError type. We encounter this error when trying to access an object’s unavailable attribute. For example, the NumPy arrays in Python have an attribute called size that returns the size of the array.

Why ‘STR’ object has no attribute ‘decode’?

The main cause of the Attributeerror: ‘str’ object has no attribute ‘decode’ is that you are already decoding the decoded strings. Decoding is the process of converting bytes object to str and encoding is the process of converting str to a bytes object. Let’s take an example and understand it.

What does 'nonetype' object has no attribute mean?

When ever you get a problems that involves a message such as " 'nonetype' object has no attribute ..." it means the same thing: you have tried to call a method on something that doesn't exist. If you try to do anything with that value, you will get this error. It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen.


1 Answers

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
like image 126
glhr Avatar answered Nov 07 '22 02:11

glhr