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).
Solution 1:
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()
, anddict.itervalues()
.Instead: use
dict.items()
,dict.keys()
, anddict.values()
respectively.
Solution 2:
In Python2, we had .items()
and .iteritems()
in dictionaries. dict.items()
returned list of tuples in dictionary [(k1,v1),(k2,v2),...]
. It copied all tuples in dictionary and created new list. If dictionary is very big, there is very big memory impact.
So they created dict.iteritems()
in later versions of Python2. This returned iterator object. Whole dictionary was not copied so there is lesser memory consumption. People using Python2
are taught to use dict.iteritems()
instead of .items()
for efficiency as explained in following code.
import timeit
d = {i:i*2 for i in xrange(10000000)}
start = timeit.default_timer()
for key,value in d.items():
tmp = key + value #do something like print
t1 = timeit.default_timer() - start
start = timeit.default_timer()
for key,value in d.iteritems():
tmp = key + value
t2 = timeit.default_timer() - start
Output:
Time with d.items(): 9.04773592949
Time with d.iteritems(): 2.17707300186
In Python3, they wanted to make it more efficient, so moved dictionary.iteritems()
to dict.items()
, and removed .iteritems()
as it was no longer needed.
You have used dict.iteritems()
in Python3
so it has failed. Try using dict.items()
which has the same functionality as dict.iteritems()
of Python2
. This is a tiny bit migration issue from Python2
to Python3
.
Solution 3:
I had a similar problem (using 3.5) and lost 1/2 a day to it but here is a something that works - I am retired and just learning Python so I can help my grandson (12) with it.
mydict2={'Atlanta':78,'Macon':85,'Savannah':72}
maxval=(max(mydict2.values()))
print(maxval)
mykey=[key for key,value in mydict2.items()if value==maxval][0]
print(mykey)
YEILDS;
85
Macon
Solution 4:
In Python2, dictionary.iteritems()
is more efficient than dictionary.items()
so in Python3, the functionality of dictionary.iteritems()
has been migrated to dictionary.items()
and iteritems()
is removed. So you are getting this error.
Use dict.items()
in Python3 which is same as dict.iteritems()
of Python2.