【问题标题】:Python - How to Count XML attribute elements with Restricted valuesPython - 如何计算具有受限值的 XML 属性元素
【发布时间】:2017-07-22 15:32:51
【问题描述】:

我试图只计算代码属性大于或等于 10 的文件标签。下面是我的代码:-

from xml.dom.minidom import parse, parseString
import xml.dom.minidom

DOMTree = xml.dom.minidom.parse("param.xml")
group = DOMTree.documentElement

code_line_10=[0,1,2,3,4,5,6,7,8,9]

num_source_file = 0
for file in group.getElementsByTagName("file"):
    if file.hasAttribute("code"):
         attribute_value = file.getAttribute("code")
         if attribute_value not in code_line:
             num_source_file += 1
print(num_source_file)

这是我正在使用的 XML 文件的摘录:-

<?xml version="1.0"?><results>
<files>
<file name="cadasta-platform/cadasta/templates/allauth/account/password_set.html" blank="5" comment="0" code="11"  language="HTML" />
  <file name="cadasta-platform/cadasta/templates/allauth/openid/login.html" blank="7" comment="0" code="11"  language="HTML" />
  <file name="cadasta-platform/cadasta/resources/tests/test_views_mixins.py" blank="4" comment="0" code="11"  language="Python" />
  <file name="cadasta-platform/cadasta/core/tests/test_translations.py" blank="2" comment="0" code="11"  language="Python" />
  <file name="cadasta-platform/cadasta/organization/urls/default/users.py" blank="2" comment="0" code="11"  language="Python" />
  <file name="cadasta-platform/cadasta/core/node_modules/bootstrap-sass/assets/stylesheets/bootstrap/mixins/_alerts.scss" blank="2" comment="1" code="11"  language="SASS" />
  <file name="cadasta-platform/cadasta/resources/tests/utils.py" blank="2" comment="0" code="11"  language="Python" />
  <file name="cadasta-platform/cadasta/core/static/js/rel_tenure.js" blank="2" comment="1" code="11"  language="Javascript" />
  <file name="cadasta-platform/cadasta/templates/party/relationship_resources_new.html" blank="3" comment="0" code="11"  language="HTML" />
  <file name="cadasta-platform/functional_tests/pages/AccountInactive.py" blank="6" comment="1" code="11"  language="Python" />
  <file name="cadasta-platform/cadasta/core/management/commands/loadsite.py" blank="3" comment="0" code="10"  language="Python" />
  <file name="cadasta-platform/cadasta/core/node_modules/bootstrap-sass/assets/stylesheets/bootstrap/mixins/_hide-text.scss" blank="2" comment="9" code="10"  language="SASS" />
  <file name="cadasta-platform/functional_tests/projects/test_project.py" blank="13" comment="109" code="0"  language="Python" />

执行上述代码后,它将计算 xml 文档中的所有文件标签,包括我要排除的标签。我做错了什么?

【问题讨论】:

    标签: python xml minidom


    【解决方案1】:

    使用支持 xpath 的库,例如 lxml,然后您可以执行以下操作:

    from lxml import etree
    tree = etree.parse("param.xml")
    print len(tree.getroot().xpath("//file[not(@code>0 and @code<10)]"))
    

    【讨论】:

      【解决方案2】:

      file.getAttribute("code") 返回 str 对象,'1' in [1]False。现在有多种方法可以解决您的问题。

      首先是不好的解决方案:

      • code_line_10=[0,1,..,9] 替换为code_line_10=['0','1',..,'9']
      • if attribute_value not in code_line: 更改为 if int(attribute_value) not in code_line:(请注意,如果 code 属性不能转换为 int 则会引发异常)

      在这两种解决方案中,算法仍然必须遍历列表中的所有项目并逐一比较项目,这需要一些时间。更快的解决方案是将值与运算符&lt;= 进行比较。因此,您可以将 if 替换为 if int(attribute_value) &gt;= 10: (同样,如果 code 属性不可转换为 int 则会引发异常)

      【讨论】:

        【解决方案3】:

        getAttribute 将值作为字符串返回。尝试类似:

        ...    
        attribute_value = file.getAttribute("code")
            if int(attribute_value) <= 10:
        ...
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-30
          • 2016-02-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多