mardi 2 juillet 2019

Finding last occurence of _sre.SRE_Pattern in a text file

I want to search for last occurence of a pattern "frame(some variable number)())" like frame1(), frame2(), frame3().... (it is not exactly a string). I'm interested in getting the variable number which will keep increasing with every occurence.

i did it by using this code

regex = re.compile('frame*[0-9]*\(\)') NoOfFrames = len(regex.findall(s))

But i think there may be more efficient way of finding it rather than listing all the occurrences and then counting it. like if we can start from last line of text file then the first occurence will be the answer. I also tried using this

m = mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) i = m.rfind(regex) # search for last occurrence of 'word' print(i) m.seek(i) # seek to the location line = m.readline() # read to the end of the line print(line)

but this will not work because what i'm searching is not exactly a string. My text file is pretty big like in gigabytes and more efficient solution will be appreciated.

Thanks!

Aucun commentaire:

Enregistrer un commentaire