[Tutor] Fwd: find second occurance of string in line

richard kappler richkappler at gmail.com
Tue Sep 8 18:56:43 CEST 2015


> Do you want to find just the second occurence in the *file* or the second
occurence within a given tag in the file (and there could be multiple such
tags)?

There are multiple objectdata lines in the file and I wish to find the
second occurence of timestamp in each of those lines.

> Is objectdata within a specific tag? Usually when parsing XML its the
tags you look for first since "lines" can be broken over multiple lines and
multiple tags can exist on one literal line.

objectdata is within a tag as is timestamp. Here's an example:

<objectdata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Logging.xsd"
version="1.0"><devicename>0381UDI1</devicename><deviceid>32</deviceid><timestamp>2015-06-18T14:28:06.570</timestamp><incr>53163</incr><tokenid>0381UDI12015-06-18T14:27:50379</tokenid><seqnb>1306</seqnb><general
oi="607360" on="379" ox="02503" oc="0" is="49787" ie="50312" lftf="N"
lfts="7" errornb="0"
iostate="DC00"><timestamp>2015-06-18T14:27:50.811</timestamp><otl
unit="inch"><value>51.45</value></otl><tt
unit="ms"><value>0</value></tt><oga
unit="inch"><value>-10.66</value></oga><devicelist>FFFFF001</devicelist><udi
udi1="2" udi2="1" udi3="20" udi4="10" udi5="0"/><uds uds1="5000"
uds2="1720" uds3="1540" uds5="OK"/><usertag1
name="SE">SE0381icdim01322142C0050.00017.20015.2IN1000000000
 0044]C00379622001900000175168000643903637408</usertag1></general><condition>AcSuccess,AC,LFT,Cubic,ValidDim</condition><barcode
cc="1" vcc="1"><codevalid
id="j"><st>0</st><wud>0</wud><cs>495</cs><bdn>18</bdn><cl>34</cl><bc>9622001900000175168000643903637408</bc><condition>FxgEpic,RefBarcode,ScannersTop,Ref,ValidRead,SortableBarcode,EpicBarcode</condition><position
unit="mm" x="119" y="544" z="375" xmin="97"
xmax="174"/><readlist>70066000</readlist><devices><device
dn="14"><dcs>101</dcs><position unit="mm" x="99" y="544"
z="439"/></device><device dn="15"><dcs>99</dcs><position unit="mm" x="174"
y="533" z="312"/></device><device dn="18"><dcs>112</dcs><position unit="mm"
x="119" y="544" z="375"/></device><device dn="19"><dcs>81</dcs><position
unit="mm" x="97" y="606" z="390"/></device><device
dn="2"><dcs>37</dcs><position unit="mm" x="117" y="539"
z="374"/></device><device dn="4"><dcs>29</dcs><position unit="mm" x="119"
y="547" z="356"/></device><device dn="3"><dcs>36</dcs><position unit="mm"
x="119" y="540"
z="358"/></device></devices></codevalid></barcode><volumetric oms1="0000"
oms2="00000000" oms3="00000001"><size unit="inch" ole="50.00" owi="17.20"
ohe="15.20"/><oa unit="degree/10"><value>-50</value></oa><obv
unit="inch3/10"><value>130720</value></obv><orv
unit="inch3/10"><value>0</value></orv><otve
unit="mm/sec"><value>0</value></otve><polygon unit="inch" oc1x="0.14"
oc1y="8.80" oc1z="15.20" oc2x="1.70" oc2y="26.20" oc2z="15.20" oc3x="50.05"
oc3y="4.32" oc3z="15.20" oc4x="51.61" oc4y="21.73"
oc4z="15.20"/></volumetric><sorterstate state="started"><speed
unit="ft/min"><value>84.06</value></speed></sorterstate><sortstate
session="SOS" sortname="2015-06-18
12:47:57"/><hostmessage>]C00379622001900000175168000643903637408</hostmessage></objectdata>

> import re

I know I don't use this in the snippet of code I sent, it is used elsewhere
in the script and I missed that when trimming.

> You should assign this once above the loops, it saves a lot of duplicated
work.

Yes, I understand, again, it is a snippet of a much larger piece of code.

> second = line.index(x,first+1)

I think that is what I was looking for, off to test now. Thanks Alan!

regards, Richard


More information about the Tutor mailing list