reading a line in file
Tim Williams
tim at tdw.net
Mon Aug 20 18:13:46 EDT 2007
On 20/08/07, Brian McCann <Brian.McCann at viziant.net> wrote:
>
> Shawn, Tim ,Jay
>
> many thanks,
>
> It looks like there are many ways this problem can be approached
>
> either by using regex or a tokens
>
> Tim I'm not familiar with your solution, but will learn about that method
> also
Hi Brian,
> buildNum = open('input.txt').read().split('build.number=')[1].split()[0]
Try this on its own:
print open('input.txt').read().split('build.number=')
See what is at the very beginning of the 2nd list item :)
My example just splits the whole file at "build.number=", so you know
the data you require is at the beginning of the 2nd list item returned
from the slice [ie: a_list[1]. You also know that data is followed by
a newline, so it will be the first item in the returned list [ie:
b_list[0] when you split a_list[1].
No need to iterate each line individually looking for a match and then
process the matching line to get at the data. You can just go
straight to the data you require.
If your requirement is for multiple items from a single file then
another method would be required.
:)
More information about the Python-list
mailing list