简体   繁体   English

在“ if”语句之后继续“ for”循环

[英]Continue “for” loop after “if” statement

I have an xml file that look like this: 我有一个看起来像这样的xml文件:

<?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. 我使用Python和cElementTree提取信息。

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". 我想提取两个字段“ show”中的信息:show =“ ARFCNs的列表= 1 2 3”和show =“ ARFCNs的列表= 5 6 7 8 9”。 So I need to have: A="1 2 3" and B="5 6 7 8 9". 所以我需要:A =“ 1 2 3”和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. 我的代码总是提取第一个列表,因为我不知道如何在“ if”语句之后的最外层循环中继续遍历“ field”。 The problem is also, that there is no unique attribute in lines where "List of ARFCNs" appears. 问题还在于,在“ ARFCN列表”出现的行中没有唯一属性。 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). “系统信息类型1”和类型2不必按上述顺序出现在xml文件中,但是“ for”循环应继续进行迭代,并且永远不要回到第一个元素,但是应检查所有“ “ field”标记的每次迭代的“”语句(xml文件中与“ if”语句匹配的行可以随机顺序出现)。

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 ). 不容易理解您想要什么...首先,请看continue语句是否对您的情况没有帮助(而不是break )。

I do not know how to continue iterating over the "field" in the outermost loop after the "if" statement. 我不知道如何在“ if”语句之后在最外层循环中继续遍历“ field”。

It looks like you are trying to break or continue two levels of nested loops at once. 看来您正在尝试一次breakcontinue两个级别的嵌套循环。 This cannot be done in one statement, as syntax such as break(2) has been proposed but refused. 不能在一个语句中完成此操作,因为已经提出但拒绝了诸如break(2)语法。 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 它应该为“ field”标签的每次迭代检查所有的“ if”语句

Then why is there a break in each of them ? 那为什么每个人都break一下呢?

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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