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?

4
  • The right thing is to use existing file format if possible such as INI (read by ConfigParser) or JSON (read by json). Commented May 8, 2010 at 15:20
  • My file is not going to be a config file, but a file that a user will upload. Commented May 8, 2010 at 16:04
  • List of string key/value pairs can be represented using INI, JSON, YAML formats whether you call it config file or not. Don't reinvent the wheel. Commented May 9, 2010 at 0:22
  • I am not reinventing the wheel and I should have been more clear earlier. My example of key, value is a simple example, the file will also contain a string of text which will not have a "key". The main part of my question was the if-elif and I didn't know about setattr. Thanks for letting me know about the ConfigParser. Commented May 9, 2010 at 17:59

2 Answers 2

4

A normal approach in Python would be something like:

for line in f:
    mo = re.match(r'^(\S+)\s+"(.*?)"\s*$',line)
    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.

Sign up to request clarification or add additional context in comments.

2 Comments

why is this the normal approach ? to my mind, parsing s not a set of regexp.
@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.
1

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.

1 Comment

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.

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.