Return lines in file that match string

Chris Rebert clp at
Tue Oct 28 19:58:06 CET 2008

On Tue, Oct 28, 2008 at 11:37 AM, Travis Kirstine
<traviskirstine at> wrote:
> I am new to python and could use some help with a fairly easy task.  I
> would like to return all lines in a file that have the string
> '<coordinates>' to a list.

from __future__ import with_statement

with open('path/to/file') as f:
    desired_lines = [line.strip() for line in f if "<coordinates>" in line]

For your reference, that uses the "with statement" and "list comprehensions".
It would also be advisable for you to read through the fine tutorial


> Regards,
> --
> Travis K.
> Toronto, Canada
> ------------------------------------------------------------
> "She knows there's no success like failure
> And that failure's no success at all."
> -Bob Dylan-
> ------------------------------------------------------------
> --

Follow the path of the Iguana...

More information about the Python-list mailing list