简体   繁体   中英

Continue “for” loop after “if” statement

I have an xml file that look like this:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="pdml2html.xsl"?>
<pdml version="0" creator="wireshark/1.12.4" time="Sat Aug 15 11:18:38 2010" capture_file=""> 
   <field name="gsm_a.dtap.msg_rr_type" showname="Message Type: System Information Type 1" size="1" pos="60" show="25" value="19"/>
    <field name="" show="Cell Channel Description" size="16" pos="61" value="0000">
      <field name="" show="List of ARFCNs = 1 2 3" size="16" pos="61" value="00000000480000000000000100000000"/>
    </field>
    <field name="gsm_a.dtap.msg_rr_type" showname="Message Type: System Information Type 2" size="1" pos="60" show="26" value="1a"/>
    <field name="" show="Neighbour Cell Description - BCCH Frequency List" size="16" pos="61" value="18000002400200000001002061900000">
      <field name="gsm_a.rr.ext_ind" showname="..0. .... = EXT-IND: The information element carries the complete BA (0)" size="1" pos="61" show="0" value="0" unmaskedvalue="18"/>
      <field name="gsm_a.rr.format_id" showname="00.. 100. = Format Identifier: bit map 0 (0x04)" size="1" pos="61" show="4" value="4" unmaskedvalue="18"/>
      <field name="" show="List of ARFCNs = 5 6 7 8 9" size="16" pos="61" value="18"/>
    </field>
</pdml>

I use Python and cElementTree to extract the information.

from xml.etree import cElementTree as ET
e = ET.parse(sys.argv[1]).getroot()
bts_id = {'A': '0', 'B': '0'}

for atype in e.iter('field'):
    if atype.attrib['name'] == "gsm_a.dtap.msg_rr_type" and atype.attrib['value'] == "19": #System Information Type 1
            for atype in e.iter('field'):
                if "List of ARFCNs =" in atype.attrib['show']:
                    bts_id['A'] = atype.attrib['show'].split("= ")[1]
                    break
    if atype.attrib['name'] == "gsm_a.dtap.msg_rr_type" and atype.attrib['value'] == "1a": #System Information Type 2
            for atype in e.iter('field'):
                if "List of ARFCNs =" in atype.attrib['show']:
                    bts_id['B'] = atype.attrib['show'].split("= ")[1]
                    break

I want extract the information in both fields "show": show="List of ARFCNs = 1 2 3" and show="List of ARFCNs = 5 6 7 8 9". So I need to have: A="1 2 3" and B="5 6 7 8 9". My code is extracting always the first list because I do not know how to continue iterating over the "field" in the outermost loop after the "if" statement. The problem is also, that there is no unique attribute in lines where "List of ARFCNs" appears. A unique attribute is only some nr of lines above. The "System Information Type 1" and Type 2 do not necessary appear in the xml file in the order as above , but the "for" loop should continue iterating and never come back to the first element, however it should check all the "if" statements for every iteration of "field" tag (the lines in xml file, that match the "if" statements, can appear in random order).

Ok, I simplify the question:

for a in range(50): #loop1
    if a=5:
        #here need to continue iterating loop1- how?
        if a=8:
            print "found 8"
    if a=22:
        #here need to continue iterating loop1- how?
        if a=28:
            print "found 28"
    if a=12:
        #here need to continue iterating loop1- how?
        if a=15:
            print "found 15"
    if a=10:
        #here need to continue iterating loop1- how?
        if a=28:
            print "again found 28"

## expected result:
#found 8
#found 28
#found 15
#again found 28
## in real life range(50) can be any list, not necessary numbers

Not easy to understand what you want... First of all, look if the continue statement does not help your case (instead of break ).

I do not know how to continue iterating over the "field" in the outermost loop after the "if" statement.

It looks like you are trying to break or continue two levels of nested loops at once. This cannot be done in one statement, as syntax such as break(2) has been proposed but refused. But you can use a flag variable, for instance

flag = 0
for i in range(10):
    if flag == 1:   # leave the outermost loop
        break
    for j in range(10):
        if j==4:    # leave the inner loop; send the message to leave the outer one
            flag = 1
            break

it should check all the "if" statements for every iteration of "field" tag

Then why is there a break in each of them ?

Does that help ? At least I get

1 2 3
5 6 7 8 9
1 2 3
5 6 7 8 9

So you just need to break at the right place now.

from xml.etree import cElementTree as ET

e = ET.parse("test.xml").getroot()

bts_id = {'A': '0', 'B': '0'}

for atype in e.iter('field'):
    if atype.attrib['name'] == "gsm_a.dtap.msg_rr_type" and atype.attrib['value'] == "19": #System Information Type 1
            for atype in e.iter('field'):
                if "List of ARFCNs =" in atype.attrib['show']:
                    print atype.attrib['show'].split("= ")[1]
    elif atype.attrib['name'] == "gsm_a.dtap.msg_rr_type" and atype.attrib['value'] == "1a": #System Information Type 2
            for atype in e.iter('field'):
                if "List of ARFCNs =" in atype.attrib['show']:
                    print atype.attrib['show'].split("= ")[1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM