N00b question: matching stuff with variables.

Sion Arrowsmith sion at viridian.paintbox
Tue Jun 29 05:51:44 EDT 2010


Stephen Hansen  <me+list/python at ixokai.io> wrote:
>On 6/28/10 10:29 AM, Ken D'Ambrosio wrote:
>> for line in file:
>>     match = re.search((seek)",(.*),(.*)", line) # Stuck here
> [ ... ]
>     name, foo, bar = line.split(",")
>     if seek in name:
>         # do something with foo and bar
>
>That'll return True if the word 'seek' appears in the first field of 
>what appears to be the comma-delimited line.

If the file input is comma-delimited, then the OP might very well
want a look at the csv module. Something like:

for line in reader(file):
    if line[0] == seek:
        # first field matches, do something with line[-1] and line[-2]
        # -- I'm not quite sure what the semantics of a pair of greedy
        # (.*)s would be

-- 
\S

   under construction




More information about the Python-list mailing list