Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: " 'dict' object has no attribute 'iteritems' "

I'm trying to use NetworkX to read a Shapefile and use the function write_shp() to generate the Shapefiles that will contain the nodes and edges, but when I try to run the code it gives me the following error:

Traceback (most recent call last):   File "C:/Users/Felipe/PycharmProjects/untitled/asdf.py", line 4, in <module>     nx.write_shp(redVial, "shapefiles")   File "C:\Python34\lib\site-packages\networkx\readwrite\nx_shp.py", line 192, in write_shp     for key, data in e[2].iteritems(): AttributeError: 'dict' object has no attribute 'iteritems' 

I'm using Python 3.4 and installed NetworkX via pip install.

Before this error it had already given me another one that said "xrange does not exist" or something like that, so I looked it up and just changed xrange to range in the nx_shp.py file, which seemed to solve it.

From what I've read it could be related to the Python version (Python2 vs Python3).

like image 763
friveraa Avatar asked May 23 '15 23:05

friveraa


People also ask

Does Iteritems have no attribute?

The Python "AttributeError: 'dict' object has no attribute 'iteritems'" occurs because the iteritems() method has been removed in Python 3. To solve the error, use the items() method, e.g. my_dict. items() , to get a view of the dictionary's items.

How do you use Iteritems in Python?

The iteritems() method generates an iterator object of the DataFrame, allowing us to iterate each column of the DataFrame. Note: This method is the same as the items() method. Each iteration produces a label object and a column object.


1 Answers

As you are in python3 , use dict.items() instead of dict.iteritems()

iteritems() was removed in python3, so you can't use this method anymore.

Take a look at Python 3.0 Wiki Built-in Changes section, where it is stated:

Removed dict.iteritems(), dict.iterkeys(), and dict.itervalues().

Instead: use dict.items(), dict.keys(), and dict.values() respectively.

like image 146
rafaelc Avatar answered Sep 21 '22 19:09

rafaelc