Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'set' object has no attribute 'items'

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,

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.

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():"

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.

like image 995
MTMA Avatar asked Aug 20 '15 14:08

MTMA


1 Answers

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 .

To define a dictionary with "MTMA" as key and 123 as value , use a colon in between them , Example -

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 = {} 
like image 75
Anand S Kumar Avatar answered Sep 22 '22 17:09

Anand S Kumar