2017-07-22 43 views
0

我想只计算与代码的文件标签属性大于或等于10下面是我的代码: -Python的 - 如何计算XML元素的属性与受限制的值

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文档中的所有文件标签,包括我想排除的选项。我是什么,我做得不对?

回答

0

getAttribute以字符串形式返回值。尝试是这样的:

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

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:(注意,如果代码属性是不可转换为INT它引发的异常)

在两种方案中的算法仍然要经过列表中的所有项目,并通过一个比较项目之一,这需要一些时间。更快的解决方案就是将该值与运营商<=进行比较。所以,你可以交替使用,如果到if int(attribute_value) >= 10:(如果再次代码属性是不可转换为INT它引发的异常)

+0

谢谢@Qeek!这工作正常 –

1

使用支持XPath的图书馆,像lxml,那么你可以做这样的事情:

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