[Tutor] how to extract data only after a certain condition is met

Eduardo Vieira eduardo.susan at gmail.com
Wed Oct 6 18:25:56 CEST 2010


The other day I was writing a script to extract data from a file from
the line where a text is found to the end of the file. The same
functionality is this sed script:
'1,/regexp/'d
I couldn't put my head to work around this and came up with a solution
using list slicing. But how can I do that? I was experimenting with a
simple list and I came up with this. I wonder if I shouldn't you a
"while" statement, but how?

a = ['m', 'a', 'r', 'i', 'g', 'o', 'l', 'd']
b = True

for letter in a:
	if letter != 'i' and b:
		continue
	elif letter == 'i':
		b = False
	else:
		print letter

Ok. This works, but I wonder if I shouldn't you a "while" statement, but how?

Of course this solution is simpler:
extracted = a[a.index("i")+1:]
But I didn't want to build a list in memory with "readlines()" in the
case of a file.

Thanks for your guidance,

Eduardo


More information about the Tutor mailing list