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

3
  • 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. Commented Feb 20, 2017 at 2: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 Commented Feb 20, 2017 at 3:05
  • Catch the exception and in the except suite print row - are the keys what you expected? Commented Feb 20, 2017 at 3:17

1 Answer 1

-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'])
Sign up to request clarification or add additional context in comments.

3 Comments

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
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]]
@LeandroPeres, 1. this is a different error, so your problem is resolved. 2. The new issue you're encountering is already discussed here: stackoverflow.com/questions/14222416/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.