0

I'm new programming on python(2.x) and even looking for hours i couldn't solve the problem. Python returns a KeyError

The (object).csv file is:

id,name,type
1,low,player

The python code is:

import csv

class Item(object):

    def setup(self, config):
        self.config = config
        self.label = config['label']
        self.name = config['name']
        self.type = config['type']
def create_item(config):
    new_item = Item()
    new_item.setup(config)
    return(new_item)

def populate():
    all_items = {}
    f = open('object.csv', 'rb')
    reader = csv.DictReader(f, delimiter = ',')
    for row in reader:
        new_item = (create_item(row))
        all_items[new_item.label] = new_item 
    return(all_items)

Python returns:

Self.type = config['type']
KeyError: 'type'

The weird thing is that both in csv and in the python code the column header doesn't contain any typing error. When i change the name of "id" column, the error returns to the new header (previously "id"). (The same happens when i add another header and try to read it)

Any help is welcome and sorry for the inconvenience. grateful

  • You have extra space around the commas so the real column names are `'id '`, `' name '` & `' type'`. Try printing a row before calling `create_item` to see it in practice. – niemmi Feb 20 '17 at 02:54
  • The extra spaces were my mistake typing the post. Sorry about that. The return when i print the row is: '{'id': '1', 'name': 'low', 'type': 'player'}' but after that, the error persists – Leandro Peres Feb 20 '17 at 03:05
  • [Catch the exception](https://docs.python.org/3/tutorial/errors.html#handling-exceptions) and in the except suite print ```row``` - are the keys what you expected? – wwii Feb 20 '17 at 03:17

1 Answers1

-1
 class Item(object):

      def setup(self, config):
          self.config = config
          self.id = config['id']
          self.name = config['name']
          self.type = config['type']

  def create_item(config):
          new_item = Item()
          new_item.setup(config)
          return(new_item)
  def populate():
          all_items = {}
          f = open('test.txt', 'r')
          reader = csv.DictReader(f, delimiter = ',')
          for row in reader:
              new_item = (create_item(row))
              all_items[new_item.id] = new_item
          return(all_items)

output:

things = populate()
things.keys()
dict_keys(['1'])
gregory
  • 10,969
  • 2
  • 30
  • 42
  • this output returns me: File "items.py", line 33, in populate f = open('files/object.csv', 'rb') RuntimeError: maximum recursion depth exceeded while calling a Python object I have no idea what it is – Leandro Peres Feb 21 '17 at 03:22
  • the same happens with f = open('files/object.txt', 'r') [the same when I swap the all_items[new_item.label] for all_items[new_item.id]] – Leandro Peres Feb 21 '17 at 03:48
  • @LeandroPeres, 1. this is a different error, so your problem is resolved. 2. The new issue you're encountering is already discussed here: http://stackoverflow.com/questions/14222416/recursion-in-python-runtimeerror-maximum-recursion-depth-exceeded-while-callin – gregory Feb 21 '17 at 04:17