By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,592 Members | 1,482 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,592 IT Pros & Developers. It's quick & easy.

Re: Return lines in file that match string

P: n/a
On Tue, Oct 28, 2008 at 11:37 AM, Travis Kirstine
<tr************@gmail.comwrote:
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
at: http://docs.python.org/tutorial/index.html

Cheers,
Chris
Regards,

--
Travis K.

Toronto, Canada
------------------------------------------------------------
"She knows there's no success like failure
And that failure's no success at all."
-Bob Dylan-
------------------------------------------------------------
--
http://mail.python.org/mailman/listinfo/python-list


--
Follow the path of the Iguana...
http://rebertia.com
Oct 28 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.