如何在Python 3中修复 "dict "对象没有 "iteritems "属性的错误?

548 阅读7分钟

目录

背后的原因:'dict'对象没有'iteritems'属性

从Python 2到Python 3有许多变化。其中一个变化是在dictionary类的属性中。dict 属性,即**dict.iteritems()**已被删除,并增加了一个新的方法来实现同样的结果。

首先,让我们试着理解为什么这个属性被删除。Python 2 的早期版本有**dict.items()**方法,它返回字典中的图元列表。考虑一下下面的例子。

# Exceuting the code in Python 2 

a={'1':"first", '2':"second" , '3':"third" }
print(a.items())

# [('1', 'first'), ('3', 'third'), ('2', 'second')]

讨论。我 们可以看到,输出是一个 图元的列表,每个图元都由一个字典的键值对作为其元素组成。这里我们可以看到,字典的所有内容都被复制到了列表中。很明显,这消耗了大量的内存。所以,在 Python 2 的后期版本中,创建了dict.iteritems() 方法来解决内存问题。这个方法返回一个 可迭代的对象。

请参考下面的例子。

# Exceuting the code in Python 2.x 

a={'1':"first", '2':"second" , '3':"third" }
print(a.iteritems())

# <dictionary-itemiterator object at 0x151800fc0cb0>

如上所示,iteritems() 方法返回一个迭代器对象。要检索这个可迭代对象的内容,你可以通过对象元素进行迭代,并在for循环的帮助下逐一打印,如下面的例子中所示。

# Exceuting the code in Python 2.x

a={'1':"first", '2':"second" , '3':"third" }
for i in a.iteritems():
    print i

输出。

('1', 'first')
('3', 'third')
('2', 'second')

由于整个字典没有被复制,这意味着在这种情况下,内存消耗较少。因此,dict.iteritems() 被证明是对以前使用的方法的一种改进,因为它提高了代码的效率。因此,Python 2 现在有两个方法dict.items()dict.iteritems() 。然而,迭代器对象的问题是迭代只能执行一次。如果需要再次进行迭代,这是不可能的。让我们看一下下面的例子来理解这个问题。

# Exceuting the code in Python 2.x
# create a dictionary
a={'1':"first", '2':"second" , '3':"third" }

res=a.iteritems()

print("Iterate for the first time")
#iterate for the first-time
for i in res:
    print i

print("Iterate for the second time")
#iterate for the second time
for j in res:
    print j

print("End of Program")

输出。

Iterate for the first time
('1', 'first')
('3', 'third')
('2', 'second')
Iterate for the second time
End of Program

讨论。从 上面的例子中可以看出,一旦迭代器被使用,它们就会被用完。因此,当第二次进行迭代时,没有任何东西被返回,因为迭代器对象已经被消耗了。因此,在Python 3中,他们想进一步改进这一点,所以该方法不是返回一个迭代器对象,而是返回一个视图对象视图对象将提供一个字典当前状态的实时视图,并且可以多次迭代。

因此在 Python 3 中,他们删除了 Python 2 的dict.items() ,然后将dict.iteritems() 方法移植到一个即兴的功能中,并将其命名为dict.item()

所以,Python 2 中的 dict.iteritems() 在功能上等同于 Python 3 中的 dict.items()。

因此,错误‘dict’ object has no attribute ‘iteritems’ ,因为在Python 3的dict 类中没有名为iteritems的属性。

例子。

# Executing the code in Python 3
# create a dictionary
a = {'1': "first", '2': "second", '3': "third"}

# Output the key-value pairs in the dictionary
for i in a.iteritems():
    print(i)

Ouptut:

# Executing the code in Python 3
# create a dictionary
a = {'1': "first", '2': "second", '3': "third"}

# Output the key-value pairs in the dictionary
for i in a.iteritems():
    print(i)

现在我们已经理解了为什么会出现这个错误,让我们在这篇文章中讨论解决这个错误的各种方法。

方法1:使用dict.items()

我们已经知道,Python 2中的 dict.iteritems() 在功能上等同于Python 3中的dict.items() ,因此,使用**dict.items()**而不是dict.iteritems() ,如下图所示。

# Executing the code in Python 3#create a dictionary
a = {'1': "first", '2': "second", '3': "third"}
# Output the key-value pairs in the dictionary
for i in a.items():
    print(i)

输出。

('1', 'first')
('2', 'second')
('3', 'third')
  • 注意,当我们说dict.itertitems() 等同于dict.items() 时,我们说它们在功能上是相同的。然而,在Python 2中dict.iteritems() 返回一个迭代器对象,在Python 3中dict.items() 返回一个视图对象。
# Exceuting the code in Python 3#create a dictionary
a = {'1':"first", '2':"second" , '3':"third" }
print(type(a.items()))
# <class 'dict_items'>

出于某种原因,如果你想要一个迭代器对象,而不是一个迭代器对象,请将视图对象转换为迭代器对象,如下所示。

iter(dict.items()
)print(iter(a.items())

# <dict_itemiterator 对象在 0x000001AE90E4B9F0>


所以,当我们执行下面的代码时,我们可以看到dict_items 类对象被转换为一个迭代器对象。

方法2:使用Python 2to3工具

如果你不想对你的脚本进行任何修改,并且在运行脚本时没有看到任何错误,你可以使用内置的2to3工具,它将自动改变Python文件中的导入。

为了使用2to3工具,打开你的终端,用下面的命令安装该工具

pip3 install 2to3

一旦你成功地安装了该工具,打开你的终端并输入下面的命令--Pip3 install 2to3。

语法:

2to3 -w <path_to_Python2File >。

例子。

比方说,我们有一个名为 dict_object_test.py的文件,内容如下。

#create a dictionary
a={'1':"first", '2':"second" , '3':"third" }

#Output the key-value pairs in the dictionary
for i in a.items():
  print(i)

如果我们想把这个文件转换为Python 3,打开终端窗口,执行下面的命令。

2to3 -w "C:\Users\admin\Desktop\Finxter\dict_object_test.py"

你会看到该工具会将文件转换为Python 3文件,如下图所示。

(venv) C:\Users\admin\Desktop\Finxter>2to3 -w "C:\Users\admin\Desktop\Finxter\dict_object_test.py"
RefactoringTool: Skipping optional fixer: buffer
RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: set_literal
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: Refactored C:\Users\admin\Desktop\Finxter\dict_object_test.py
--- C:\Users\admin\Desktop\Finxter\dict_object_test.py  (original)
+++ C:\Users\admin\Desktop\Finxter\dict_object_test.py  (refactored)
@@ -1,4 +1,4 @@
a={'1':"first", '2':"second" , '3':"third" }

-for i in a.iteritems():
+for i in a.items():
    print(i)
RefactoringTool: Files that were modified:
RefactoringTool: C:\Users\admin\Desktop\Finxter\dict_object_test.py

注意,将对文件进行修改,并在相同的文件位置上保留文件的备份。

方法3:使用第三方模块

有一些第三方模块可以在Python程序中被导入和使用。这些模块有助于得出与Python 2和3版本都兼容的代码。

⦿六库模块

six library module 有助于编写与Python 2.5+和3+版本兼容的代码。它有一个名为iteritems() 的方法可以使用。

要使用 six 模块的 iteritems 方法,你必须先安装 six 模块。要做到这一点,请打开你的终端并输入以下命令

pip install six

要使用该模块,在你的程序中导入 six 模块,如下所示

import six

语法:

six.iteritems(dict)

这里。 dict是你要使用的字典。

例子。 让我们使用同样的例子,使用six.iteritems() 方法。

import six

# create a dictionary
a = {'1':"first", '2':"second" , '3':"third" }

# Output the key-value pairs in the dictionary
for i in six.iteritems(a):
  print(i)

输出。

('1', 'first')
('2', 'second')
('3', 'third')

如上例所示,没有错误。

⦿⦿Thefuture.utils库模块

future模块是另一个第三方库模块,可以用来编写与Python版本、2.6+和3.3+兼容的代码。future 模块有一个名为 utils 的子模块,包含 iteritems() 方法。

要使用 future 模块,我们必须首先安装它。要做到这一点,打开你的终端窗口并输入以下命令。

pip install future

为了使用该模块,在你的程序中导入future模块,如下所示

import future.utils as fu

语法:

fu.iteritems(dict)

这里 dict是你想使用的字典。

例子。

让我们使用同样的例子,使用 fu.iteritems() 方法。

import future.utils as fu

# create a dictionary
a = {'1': "first", '2': "second", '3': "third"}

# Output the key-value pairs in the dictionary
for i in fu.iteritems(a):
    print(i)

# ('1', 'first')('2', 'second')('3', 'third')

正如在上面的例子中看到的,没有抛出错误。

总结

在本教程中,我们讨论了不同的方法来解决 **"dict "对象没有属性 "iteritems "**的错误 **。我们希望这对你有帮助,你现在可以避免在你的代码中得到这个错误。 请继续关注订阅**更多此类技巧和窍门。

快乐的Python!

帖子的作者。 Anusha Pai和Shubham Sayon

The postHow to Fix the error 'dict' object has no attribute 'iteritems' in Python 3?first appeared onFinxter.