[Tutor] find second occurance of string in line

richard kappler richkappler at gmail.com
Tue Sep 8 18:00:35 CEST 2015


I need to find the index of the second occurance of a string in an xml file
for parsing. I understand re well enough to do what I want to do on the
first instance, but despite many Google searches have yet to find something
to get the index of the second instance, because split won't really work on
my xml file (if I understand split properly) as there are no spaces.
Specifically I'm looking for the second <timestamp> in an objectdata line.
Not all lines are objectdata lines, though all objectdata lines do have
more than one <timestamp>.

What I have so far:

import re

with open("example.xml", 'r') as f:
    for line in f:
        if "objectdata" in line:
            if "<timestamp>" in line:
                x = "<timestamp>"
                first = x.index(line)
                second = x[first+1:].index(line)
                print first, second
            else:
                print "no timestamp"
        else:
            print "no objectdata"

my traceback:

Traceback (most recent call last):
  File "2iter.py", line 10, in <module>
    first = x.index(line)
ValueError: substring not found



-- 

All internal models of the world are approximate. ~ Sebastian Thrun


More information about the Tutor mailing list