tags:

views:

116

answers:

2

My input file is going to be something like this

key "value"
key "value"
... the above lines repeat

What I do is read the file contents, populate an object with the data and return it. There are only a set number of keys that can be present in the file. Since I am a beginner in python, I feel that my code to read the file is not that good

My code is something like this

ObjInstance = CustomClass()
fields = ['key1', 'key2', 'key3']

    for field in fields:
        for line in f:
            if line.find(field) >= 0:
                if pgn_field == 'key1':
                    objInstance.DataOne = get_value_using_re(line)
                elif pgn_field == 'key2':
                    objInstance.DataTwo = get_value_using_re(line)

return objInstance;

The function "get_value_using_re" is very simple, it looks for a string in between the double quotes and returns it.

I fear that I will have multiple if elif statements and I don't know if this is the right way or not.

Am I doing the right thing here?

+3  A: 

A normal approach in Python would be something like:

for line in f:
    mo = re.match(r'^(\S+)\s+"(.*?)"\s*$')
    if not mo: continue
    key, value = mo.groups()
    setattr(objInstance, key, value)

If the key is not the right attribute name, in the last line line in lieu of key you might use something like translate.get(key, 'other') for some appropriate dict translate.

Alex Martelli
beautiful :) Thanks
iHeartDucks
why is this the normal approach ? to my mind, parsing s not a set of regexp.
dzen
@dzen, for _sufficiently simple_ parsing tasks REs are the cat's pajamas: fast, compact, convenient. `pyparsing` and other tools are fine when more power is needed (e.g. to match nested parentheses), but would be overkill here, thus, not the normal approach; extensive string operations (`find` calls, slicing, etc) are often slower and more verbose.
Alex Martelli
+3  A: 

I'd suggest looking at the YAML parser for python. It can conveniently read a file very similar to that and input it into a python dictionary. With the YAML parser:

import yaml
map = yaml.load(file(filename))

Then you can access it like a normal dictionary with map[key] returning value. The yaml files would look like this:

key1: 'value'
key2: 'value'

This does require that all the keys be unique.

Jonathan Sternberg
I don't think that the YAML parser will work for me, since the file can contain a some text which does not have a key.
iHeartDucks