[Tutor] search function

Conrad linux-user at softhome.net
Thu Jul 29 19:55:30 CEST 2004


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 display_var(variables):
        var_disp = re.compile(r'%s' % (variables.replace(',','|')))
        line = config_file.readline()
        var_value = re.compile(r'"[^"]*?"$')
        var_name = re.compile(r'".*?"')
        while line != '':
                if var_disp.search(line):
                        value = var_value.search(line)
                        var = var_name.search(line)
                        print "%s: %s" % (var.group()[1:-1],
value.group()[1:-1])
                else:
                        pass
                line = config_file.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



More information about the Tutor mailing list