From: Stephen Hansen on
On 6/29/10 2:51 AM, Sion Arrowsmith wrote:
> Stephen Hansen<me+list/python(a)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/

From: Tim Golden on
On 29/06/2010 15:14, Stephen Hansen wrote:
> 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.

The most common reason is that it caters for all the nastiness of embedded
commas, quotes etc. which you'd otherwise have to end up reinventing. If
you know your data's just a string of numbers, then just use s.split (",").

TJG