richard kappler
2015-09-08 16:00:35 UTC
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
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
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
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor