37

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.

MTMA
  • 373
  • 1
  • 3
  • 5
  • Well the error itself is saying there is no attribute called `items()`. So try and remove `.items()` from `self.changes.items()` http://docs.python.org/3/library/stdtypes.html#dictionary-view-objects Also, why did you do `self.changes = {self.key:self.val}`? If you are just trying to reference a dictionary, adding `self.key:self.val` is not necessary as `self.changes = {}` is enough. – awbemauler Aug 20 '15 at 14:32
  • When I take away the .items() it returns a ValueError that says "need more than 0 values to unpack". I will try leaving the dictionary empty. – MTMA Aug 20 '15 at 14:37
  • Yes, because you have nothing in your key and value. You initiated those variables with nothing. – awbemauler Aug 20 '15 at 14:38
  • Can you specify what you're passing into the function as `self`? – awbemauler Aug 20 '15 at 14:44
  • I'm not totally sure what you mean by that. (As explained above I'm pretty amateur), but when I don't include the "self." it will give me another error. – MTMA Aug 20 '15 at 14:49
  • What are you doing to call the function? – awbemauler Aug 20 '15 at 14:50
  • The SubmitEdit function is called when a button in the class is pushed. Is this what you were looking for? – MTMA Aug 20 '15 at 14:53

1 Answers1

70

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 = {}
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176