> Question:
> Is there any function where I can specify to python
> buit-in function to select specific line (such as
> starting from segment: page 22 TO the next new line)
> instead of the whole lines until EOF.
> e.g.:
> a = readlines (From , TO )
Well, Kumar, I don't know what you heard before, but you can always
write your own modified "read" module and call it, say, "myread", with
parameters fileobj (self explanatory: an open file), frompat (the
pattern signifying where in the file you want to start collecting
data), and endpat (the pattern signifying where in the file you want
to stop collecting data).
### Start Code ###
def myread(fileobj, frompat, endpat):
fileobj.seek(0) ## ok, first make sure you're at the start of the
file
readl = []
try:
while 1:
line = fileobj.next()
if line == frompat: ## if line is the starting
pattern,
readl.append(line) ## record it
break ## but stop reading lines
except StopIteration: ## if you've gone through the entire file
without finding
print "Sorry, could not find your starting pattern." ## the
start pattern
return False
try:
while 1:
line = fileobj.next()
if line != endpat: ## very similar to before, only
now you record until
readl.append(line) ## you reach the end
pattern, not when you
else: ## reach the starting pattern
readl.append(line)
break
except StopIteration:
print "Sorry, could not find your ending pattern."
print "Here's the file past your starting pattern, though: "
return readl
return readl
### End Code ###
And that's it!
For your situation, you'd probably use it like:
myread("segmentfile.txt", "Segment:Page 22\n", "Segment:Page 23\n")
and modify the module so that it does not record the last line, ie
take out the 3rd "readl.append(line)" line at line 19 in the module
HTH,
Orri.
--
Email: [EMAIL PROTECTED]
AIM: singingxduck
Programming Python for the fun of it.
_______________________________________________
Tutor maillist - [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor