1

我正在一个学校项目中从文本文件中提取特定行。我用方法创建了一个类。代码得到了我现在想要的行,但它不会继续得到另一个行块。我正在尝试修改我的代码以使其继续执行此过程,直到文件的 ens 为止。我的具体问题是如何制作lines[i]不是每次都从零开始,而是从最后一个方法停止的地方继续。(我可以保存i 值但不知道如何将i值传递给以下方法):

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()
4

0 回答 0