python - 错误 : "' dict' object has no attribute 'iteritems' "

标签 python python-3.x dictionary

我正在尝试使用 NetworkX 读取 Shapefile 并使用函数 write_shp() 生成将包含节点和边的 Shapefile,但是当我尝试运行它给出的代码时我出现以下错误:

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'

我正在使用 Python 3.4 并通过 pip install 安装了 NetworkX。

在此错误之前,它已经给了我另一个说“xrange 不存在”或类似的东西,所以我查了一下,只是将 xrange 更改为 range 在 nx_shp.py 文件中,这似乎解决了它。

根据我的阅读,它可能与 Python 版本(Python2 与 Python3)有关。

最佳答案

在 python3 中,使用 dict.items() 而不是 dict.iteritems()

iteritems()在python3中被移除了,所以你不能再使用这个方法了。

看看 Python 3.0 Wiki Built-in Changes部分,其中说明:

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

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

关于python - 错误 : "' dict' object has no attribute 'iteritems' ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30418481/

相关文章:

python - 为什么这段代码不起作用(我是编程新手,python)

用于谷歌游戏商店抓取的 Python API

python - 如何按多个键对对象进行排序?

python - 更改 New_dictionary 中键的名称,其中 New_dictionary 作为 Main_dictionary 的值

python - 如何根据字典值对列表进行排序

python - 单击鼠标并在视频中的鼠标位置绘制固定矩形(python_opencv)

python - GAE 中的 worker 角色和 Web 角色对应

python - 装饰方法赋值前引用的局部变量

python - 检查文本中是否存在大量关键字

Python 请求发布一个文件