1

我有这个代码,我想将具有相同标签的动物分组到一组前。标记等。但在我的代码<dog><dog><dogs><dog/><dog/></dogs>,我不知道为什么输出没有动物。

输出:

<root>
       <zoo>
           <some_tag/><some_diff/>
       </zoo>
       <zoo>
           <b/><o/>
       </zoo>
 </root>

代码:

 xml = '`<root>
                  <zoo>
                      <some_tag/><some_diff/>
                      <dog/><dog/>
                      <cat/><cat/><cat/>
                  </zoo>
                  <zoo>
                      <b/><o/>
                      <dog/><dog/>
                      <cat/><cat/><cat/><cat/>
                  </zoo>
            </root>`'

from lxml import etree as et
root = et.fromstring(xml)
node = root.findall('./zoo')
j = False
k = False
for zoo in node:
    for animal in zoo:
        if 'dog' in animal.tag:
            if not j:
                dogs = et.SubElement(zoo,'dogs')
            dogs.append(animal)
            j = True
        if 'cat' in animal.tag:                        
            if not k:
                cats = et.SubElement(zoo,'cats')            
            cats.append(animal)
            k = True

    k = False
    j= False  
4

1 回答 1

1

我对您的脚本进行了一些修改,它对我有用.. 看看:

xml = '''<root>
                  <zoo>
                      <some_tag/>
                      <some_diff/>
                      <dog/>
                      <dog/>
                      <cat/>
                      <cat/>
                      <cat/>
                  </zoo>

                  <zoo>
                      <b/>
                      <o/>
                      <dog/>
                      <dog/>
                      <cat></cat>
                      <cat></cat>
                  </zoo>
            </root>'''

from lxml import etree as et


root = et.fromstring(xml)

# The below 3 lines have the same effect, use whichever you like
node = root.findall('./zoo')
node = list( root.getchildren() )
node = root.getchildren()


dogs_flag = False
cats_flag = False

for zoo in node:

    # Resetting the flags in each iteration, otherwise, you will 
    # have all the cats and dogs inside one zoo element ... try it yourself
    dogs_flag = False
    cats_flag = False

    for animal in zoo:

        if 'dog' == animal.tag:
            if not dogs_flag:
                dogs = et.SubElement(zoo,'dogs')
                dogs_flag = True    # I think this is a better place to set your flag                

            dogs.append(animal)


        if 'cat' == animal.tag:                        
            if not cats_flag:
                cats = et.SubElement(zoo,'cats')            
                cats_flag = True

            cats.append(animal)


print et.tostring(root, pretty_print = True)  

这会给你这个输出

        <root>
              <zoo>
                  <some_tag/>
                  <some_diff/>
                  <dogs>
                      <dog/>
                      <dog/>
                  </dogs>
                  <cats>
                      <cat/>
                      <cat/>
                      <cat/>
                  </cats>
              </zoo>

              <zoo>
                  <b/>
                  <o/>
                  <dogs>
                      <dog/>
                      <dog/>
                  </dogs>
                  <cats>
                      <cat/>
                      <cat/>
                  </cats>
              </zoo>
        </root>
于 2013-10-09T01:26:40.857 回答