AttributeError: ‘str’ object has no attribute ‘items’ in Python

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

When working with Python, you may come across strings that look like dictionaries. If you try using the items() attribute on them, you will get the error “AttributeError: ‘str’ object has no attribute ‘items’” in Python. This article will explain how the error occurs and how to fix it.

The reason of this error

From the error message, you may somewhat understand the error. The error occurs because you apply the items attribute to a string while the attribute is only available to the dictionary data type.

Code:

string = '{"key1": "value1", "key2": "value2", "key3": "value3"}'
 
print(string.items())

Result:

Traceback (most recent call last):
   line 3, in <module>
    print(string.items())
AttributeError: 'str' object has no attribute 'items'

You may have misunderstood when seeing a string looking like a dictionary. Keep reading, and we will show you some common situations where you can make mistakes and solve them.

Solutions to the error

Because the items() attribute is only available in the dictionary objects. You must convert the string into a dictionary to apply the attribute. We will introduce you to two methods to transform a string into a dictionary.

Use the json.loads() function

You likely face the string like that when working with json format. In this case, use the loads() function to convert the json string into a dictionary. Take a look at the example below for more details. 

Code:

import json

jsonString = '{"key1": "value1", "key2": "value2", "key3": "value3"}'
 
# Transform the json string into a dictionary
myDict = json.loads(jsonString)
 
print("The type of myDict is:", type(myDict))
print(f"The dictionary's items are: \n{myDict.items()}")

Result:

The type of myDict is: <class 'dict'>
The dictionary's items are: 
dict_items([('key1', 'value1'), ('key2', 'value2'), ('key3', 'value3')])

Use the eval() function 

You also use the eval() function to convert a string into a dictionary. But if you work with json, do not use the eval() function, although they can convert the string into a dictionary.

Code:

string = '{"key1": "value1", "key2": "value2", "key3": "value3"}'
 
# Transform the string into a dictionary
myDict = eval(string)
 
print("The type of myDict is:", type(myDict))
print(f"The dictionary's items are: \n{myDict.items()}")

Result:

The type of myDict is: <class 'dict'>
The dictionary's items are: 
dict_items([('key1', 'value1'), ('key2', 'value2'), ('key3', 'value3')])

Summary

In summary, the error “AttributeError: ‘str’ object has no attribute ‘items'” in Python occurs when you apply the items() attribute to a string that looks like a dictionary. To eliminate the error, convert the string into a dictionary by the json.loads() function if you work with JSON or the eval() function when working with normal strings.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *