2017-07-22 40 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)]"))