【问题标题】:Parsing xml with lxml in python 3在python 3中用lxml解析xml
【发布时间】:2013-10-08 22:37:28
【问题描述】:

我有这个代码,我想将具有相同标签的动物分组到一组前。 tags<dog><dog> to <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  

【问题讨论】:

  • 请改写你的问题..不太清楚
  • 我改了,是不是更好?
  • 是..好多了:))

标签: xml python-3.x lxml


【解决方案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>

【讨论】:

  • 是的,就是这样。你的代码看起来比我的好多了。但我的问题是比较。我的代码:animal.tag 中的 'dog' 和你的:'dog' == animal.tag,你知道我的比较不好的原因吗?
  • @dusan,通常in用于在字符串/列表/元组/集合中查找一些对象(python中的一切都是对象),但不用于对象之间的比较..做一些实验使用in 将帮助您了解差异...祝您好运。
猜你喜欢
  • 2018-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-22
  • 2012-12-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多