1

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()
tshepang
  • 12,111
  • 21
  • 91
  • 136
  • Can you edit your post to include the full code instead of pseudocode? It's currently difficult to identify what the problem is, since we don't really have a good idea of what you're trying to do, or what your code actually does. – Michael0x2a Apr 18 '13 at 22:20
  • Possible duplicate: [how to jump to a particular line in a huge text file?](http://stackoverflow.com/questions/620367/python-how-to-jump-to-a-particular-line-in-a-huge-text-file) – Mariano Apr 23 '13 at 03:33
  • What do you mean by 'passing i to the following method'? Which method? –  Apr 24 '13 at 09:11
  • Can you provide examples of what you want in your project? From your description, the problem appears to be a very simple one - something that doesnt merit a `Class` for implementation. – ssm Jan 31 '14 at 08:39

0 Answers0