N00b question: matching stuff with variables.
Stephen Hansen
me+list/python at ixokai.io
Tue Jun 29 10:14:43 EDT 2010
On 6/29/10 2:51 AM, Sion Arrowsmith wrote:
> 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
>
True: but I've personally never seent he point of the csv module unless
we're talking about a more complicated csv format, such as one with
quoting in fields. I don't know if that's what the OP is working with,
but good point: csv might be a good approach if this is more complicated
format then just a line with a couple commas.
--
... Stephen Hansen
... Also: Ixokai
... Mail: me+list/python (AT) ixokai (DOT) io
... Blog: http://meh.ixokai.io/
More information about the Python-list
mailing list