[Tutor] search function (original) (raw)
orbitz orbitz at ezabel.com
Fri Jul 30 09:08:57 CEST 2004
- Previous message: [Tutor] search function
- Next message: [Tutor] search function
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Why not just use a python file as your config? That is what I do, then I just import it.
You can also use wat Steve suggested, but IMO just using python is more powerful since you can do things like tuples and what not trivially
Conrad wrote:
I'm writing a script that extracts values from config files in the form of:
"variable"="value" "variable1"="value" I wrote this function, which takes a comma delimited list of variables to search for. (variable1,variable2,....), and then searches through each line of the config file, checking if the variable is there, and then printing out the variable found and the value: def displayvar(variables): vardisp = re.compile(r'%s' % (variables.replace(',','|'))) line = configfile.readline() varvalue = re.compile(r'"[^"]*?"$') varname = re.compile(r'".*?"') while line != '': if vardisp.search(line): value = varvalue.search(line) var = varname.search(line) print "%s: %s" % (var.group()[1:-1], value.group()[1:-1]) else: pass line = configfile.readline() There are a few things that are bugging me about this. One is the heavy use of regular expressions, and two is that im using [1.-1] to strip the qoutes. Can anyone point out how to make this more pythonic? Your time is appreciated, Conrad
Tutor maillist - Tutor at python.org http://mail.python.org/mailman/listinfo/tutor
- Previous message: [Tutor] search function
- Next message: [Tutor] search function
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]