AttributeError: 'str' object has no attribute 'decode'

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

AttributeError: 'str' object has no attribute 'decode' #

The Python "AttributeError: 'str' object has no attribute 'decode'" occurs when we call the decode() method on a string that has already been decoded from bytes. To solve the error, remove the call to the decode() method as the string is already decoded.

attributeerror str object has no attribute decode

Here is an example of how the error occurs.

main.py
my_str = 'hello world' # ⛔️ AttributeError: 'str' object has no attribute 'decode'. Did you mean: 'encode'? decoded = my_str.decode('utf-8')

The issue in the code sample is that we are trying to decode a string object.

Decoding is the process of converting a bytes object to a string and since we already have a string, we don't have to call the decode() method on it.

To solve the error, remove the call to the decode() method.

main.py
my_str = 'hello world' print(my_str) # 👉️ "hello world"

If you aren't sure where you have a bytes object or a string, use a try/except statement to handle the possible AttributeError.

main.py
my_str = 'hello world' try: decoded = my_str.decode('utf-8') print(decoded) except AttributeError: pass # 👉️ this runs

We try to decode the value and if it doesn't have a decode() attribute (it's not a bytes object), we handle the AttributeError.

Encoding is the process of converting a string to a bytes object and decoding is the process of converting a bytes object to a string.

In other words, you can use the str.encode() method to go from str to bytes and bytes.decode() to go from bytes to str.

main.py
my_text = 'hello' my_binary_data = my_text.encode('utf-8') print(my_binary_data) # 👉️ b'hello' my_text_again = my_binary_data.decode('utf-8') print(my_text_again) # 👉️ 'hello'

The str.encode() method is the opposite of bytes.decode() and returns a bytes representation of the unicode string, encoded in the requested encoding.

You can also use bytes(s, encoding=...) and str(b, encoding=...).

main.py
my_text = 'hello' my_binary_data = bytes(my_text, encoding='utf-8') print(my_binary_data) # 👉️ b'hello' my_text_again = str(my_binary_data, encoding='utf-8') print(my_text_again) # 👉️ 'hello'

The str class returns a string version of the given object. If an object is not provided, the class returns an empty string.

The syntax for using the bytes class is the same, except that a b prefix is added.

Ever since Python 3, the language uses the concepts of text and binary data instead of unicode strings and 8-bit strings.

All text in Python is unicode, however encoded Unicode is represented as binary data.

You can use the str type to store text and the bytes type to store binary data.

In Python 3 you can no longer use u'...' literals for Unicode text because all strings are now unicode.

However, you must use b'...' literals for binary data.

A good way to start debugging is to print(dir(your_object)) and see what attributes a string has.

Here is an example of what printing the attributes of a string looks like.

main.py
my_string = 'hello world' # [ 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', # 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', # 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', # 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', # 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', # 'title', 'translate', 'upper', 'zfill'] print(dir(my_string))

If you pass a class to the dir() function, it returns a list of names of the class's attributes, and recursively of the attributes of its bases.

If you try to access any attribute that is not in this list, you would get the "AttributeError: str object has no attribute error".

Since decode() is not a method implemented by strings, the error is caused.

Conclusion #

The Python "AttributeError: 'str' object has no attribute 'decode'" occurs when we call the decode() method on a string that has already been decoded from bytes. To solve the error, remove the call to the decode() method as the string is already decoded.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.