Python AttributeError: 'set' 对象没有属性 'items'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32121015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AttributeError: 'set' object has no attribute 'items'
提问by MTMA
I am very new to python and have been trying to teach myself as I go (not the best method this deep into python but for time's sake I need too). The modules I've imported are Tkinter and csv. Let me know if you have any questions,
我对python非常陌生,并且一直在尝试自学(不是深入python的最佳方法,但为了时间的缘故,我也需要)。我导入的模块是 Tkinter 和 csv。如果您有任何问题,请告诉我
For the sake of brevity I am not going to post my entire code on here but I will include the entire error and indicate the line that the error is applying to. Everything that is below is in a class called MainApp.
为简洁起见,我不会在这里发布我的整个代码,但我会包含整个错误并指出错误适用的行。下面的所有内容都在一个名为 MainApp 的类中。
def SubmitEdit(self):
self.key=""
self.val=""
new_rows = []
self.changes = {self.key:self.val}
with open("info.csv",'rb') as f:
reader = csv.reader(f):
for row in reader:
new_row = row
for field in row:
if field == "NAME":
print "groovy"
for (self.key,self.val) in self.changes.items():
new_row = [ x.replace(self.key,self.val) for x in new_row]
new_rows.append(new_row)
with open("info.csv","wb") as f:
writer = csv.writer(f):
writer.writerows(new_rows)
I wrote this code out separately to make sure it worked before putting it in the program and it worked perfectly, but when I put it in the class and made the changes (I thought) I needed to make to the lines of code / variables it didn't work. So that leads me to believe that I'm just correcting something incorrectly.
我单独编写了这段代码,以确保在将其放入程序之前它可以正常工作并且它运行良好,但是当我将它放入类中并进行更改时(我认为)我需要对代码行/变量进行更改没有用。所以这让我相信我只是在纠正错误。
Here is the error:
这是错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py",line 1536, in__call__
return self.func(*args)
File "C:\Python27\draft.py", line 328, in SubmitEdit
for (self.key,self,val) in self.changes:
AttributeError: 'set' object has no attribute 'items'
Where line 328 refers to the line: "or (self.key,self.val) in self.changes.items():"
其中第 328 行是指以下行:“or (self.key,self.val) in self.changes.items():”
I have changed around (I feel like) the presence or absence of "self." for all variables but I just can't get it to work.
我已经改变了(我觉得)“自我”的存在或不存在。对于所有变量,但我无法使其正常工作。
EDIT: I altered the code to look like this:
编辑:我将代码更改为如下所示:
def SubmitEdit(self):
new_rows = []
self.changes = {"MTMA",123}
with open("info.csv",'rb') as f:
reader = csv.reader(f):
for row in reader:
new_row = row
for field in row:
if field == "NAME":
print "groovy"
for (key,val) in self.changes.items():
new_row = [ x.replace(key,val) for x in new_row]
new_rows.append(new_row)
with open("info.csv","wb") as f:
writer = csv.writer(f):
writer.writerows(new_rows)
as per the comments, but still get the exact same error.
根据评论,但仍然得到完全相同的错误。
采纳答案by Anand S Kumar
As you can see from the latest updated code -
正如您从最新更新的代码中看到的那样-
self.changes = {"MTMA",123}
When you define self.changes as above , you are actually defining a set , not a dictionary , since you used ',' (comma) instead of colon , I am pretty sure in your actual code you are using comma itself , not colon .
当你像上面那样定义 self.changes 时,你实际上是在定义一个集合,而不是一个字典,因为你使用了 ',' (逗号)而不是冒号,我很确定在你的实际代码中你使用的是逗号本身,而不是冒号。
To define a dictionary with "MTMA" as key and 123 as value , use a colon in between them , Example -
要定义以“MTMA”为键和 123 为值的字典,请在它们之间使用冒号,例如 -
self.changes = {"MTMA":123}
Do similarly in your actual code as well.
在您的实际代码中也做类似的事情。
If what you want is an empty dictionary , define it as -
如果您想要的是一个空字典,请将其定义为 -
self.changes = {}