I'm working in a school project to to extract specific lines from a text fie.
I created a class with methods. the code gets the lines that i want exactly for now but it does not continue to get another block of lines. I'm trying to modify my code to make it continue doing this process until the ens of the file. My specific question is how to make lines[i]
does not start every time from zero but instead to continue from where last method stopped. (I can save the i
value but don't know how to pass i
value to the following method):
import re
f=open("C:/Users/zeretti/Google Drive/IUPUI/2nd semester/230/proejct/textout.txt","r")
lines = f.readlines()
f.close()
class Text(object):
def __init__(self,
startline=0, i=0, endline=0, keepgoing=True, current=0, metaline=0,
matchTotal="", printedLines=0, h=0):
object.__init__(self)
#build all your proprties here self.something
self.startline = startline
self.endline = endline
self.i = i
self.keepgoing=keepgoing
self.current=current
self.metaline=metaline
self.printedLine=printedLines
self.h=h
self.setCurrent()
def processing(self):
keepgoing=True
self.current=self.i
while keepgoing:
if 'Processing' in lines[self.current]:
self.startLine=self.current
processing =lines[self.current].strip('Processing 00000000.tx.1: ')
print processing #(sentence of process)
#print "it's in index %d" %self.startLine
self.current=self.current+1
return ""
def Meta_Candidates(self):
keepgoing=True
self.current=self.i
while keepgoing:
if 'Meta Candidates' in lines[self.current]:
self.metaline=self.current
phrase=lines[self.current-3]
print phrase #(the candidiate sentence)
total=re.search('\d',lines[self.current], re.S)
self.matchTotal=total.group()
self.matchTotal=int(self.matchTotal)
#print self.matchTotal
keepgoing=False
self.current=self.current+1
return ""
def printed_lines(self):
keepgoing=True
self.current=self.i-1
while keepgoing:
if 'Meta Candidates' in lines[self.current]:
self.printedLines=lines[self.current+self.matchTotal]
print self.printedLines
#print self.matchTotal
self.matchTotal=self.matchTotal-1
if self.matchTotal==0:
keepgoing=False
self.current=self.current+1
self.h=self.current
return ""
def setCurrent(self):
if self.current==0:
print '0'
pass
elif self.current>0:
self.current=self.h
print 'above 0'
return self.current
def main():
for c in range (3):
output=Text()
output.processing()
output.Meta_Candidates()
output.printed_lines()
output.setCurrent()
main()