samedi 7 novembre 2015

How to recover only the second instance of a string in a text file?

I have a large number of text files (>1000) with the same format for all.

The part of the file I'm interested in looks something like:

# event 9
num:     1
length:      0.000000
otherstuff: 19.9 18.8 17.7
length: 0.000000 176.123456

I need only the second index value of the second instance of the defined variable, in this case length. Is there a pythonic way of doing this (i.e. not sed)?

My code looks like:

with open(wave_cat,'r') as catID:
        for i, cat_line in enumerate(catID):
            if not len(cat_line.strip()) == 0:
                line    = cat_line.split()
                #replen = re.sub('length:','length0:','length:')
                if line[0] == '#' and line[1] == 'event':
                    num = long(line[2])
                elif line[0] == 'length:':
                    Length = float(line[2])

Aucun commentaire:

Enregistrer un commentaire