• <i id='QQLgP'><tr id='QQLgP'><dt id='QQLgP'><q id='QQLgP'><span id='QQLgP'><b id='QQLgP'><form id='QQLgP'><ins id='QQLgP'></ins><ul id='QQLgP'></ul><sub id='QQLgP'></sub></form><legend id='QQLgP'></legend><bdo id='QQLgP'><pre id='QQLgP'><center id='QQLgP'></center></pre></bdo></b><th id='QQLgP'></th></span></q></dt></tr></i><div id='QQLgP'><tfoot id='QQLgP'></tfoot><dl id='QQLgP'><fieldset id='QQLgP'></fieldset></dl></div>

      <small id='QQLgP'></small><noframes id='QQLgP'>

        • <bdo id='QQLgP'></bdo><ul id='QQLgP'></ul>
        <legend id='QQLgP'><style id='QQLgP'><dir id='QQLgP'><q id='QQLgP'></q></dir></style></legend>

        <tfoot id='QQLgP'></tfoot>
      1. Python ElementTree 模块:使用“find"、“findall&qu

        时间:2023-09-10
          <tbody id='InWXu'></tbody>
          <bdo id='InWXu'></bdo><ul id='InWXu'></ul>

          1. <i id='InWXu'><tr id='InWXu'><dt id='InWXu'><q id='InWXu'><span id='InWXu'><b id='InWXu'><form id='InWXu'><ins id='InWXu'></ins><ul id='InWXu'></ul><sub id='InWXu'></sub></form><legend id='InWXu'></legend><bdo id='InWXu'><pre id='InWXu'><center id='InWXu'></center></pre></bdo></b><th id='InWXu'></th></span></q></dt></tr></i><div id='InWXu'><tfoot id='InWXu'></tfoot><dl id='InWXu'><fieldset id='InWXu'></fieldset></dl></div>

              • <tfoot id='InWXu'></tfoot>

                <small id='InWXu'></small><noframes id='InWXu'>

                  <legend id='InWXu'><style id='InWXu'><dir id='InWXu'><q id='InWXu'></q></dir></style></legend>
                1. 本文介绍了Python ElementTree 模块:使用“find"、“findall"方法时如何忽略 XML 文件的命名空间来定位匹配元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想使用findall"的方法在ElementTree模块中定位源xml文件的一些元素.

                  I want to use the method of "findall" to locate some elements of the source xml file in the ElementTree module.

                  但是,源 xml 文件 (test.xml) 具有命名空间.我将 xml 文件的一部分截断为示例:

                  However, the source xml file (test.xml) has namespace. I truncate part of xml file as sample:

                  <?xml version="1.0" encoding="iso-8859-1"?>
                  <XML_HEADER xmlns="http://www.test.com">
                      <TYPE>Updates</TYPE>
                      <DATE>9/26/2012 10:30:34 AM</DATE>
                      <COPYRIGHT_NOTICE>All Rights Reserved.</COPYRIGHT_NOTICE>
                      <LICENSE>newlicense.htm</LICENSE>
                      <DEAL_LEVEL>
                          <PAID_OFF>N</PAID_OFF>
                          </DEAL_LEVEL>
                  </XML_HEADER>
                  

                  示例python代码如下:

                  The sample python code is below:

                  from xml.etree import ElementTree as ET
                  tree = ET.parse(r"test.xml")
                  el1 = tree.findall("DEAL_LEVEL/PAID_OFF") # Return None
                  el2 = tree.findall("{http://www.test.com}DEAL_LEVEL/{http://www.test.com}PAID_OFF") # Return <Element '{http://www.test.com}DEAL_LEVEL/PAID_OFF' at 0xb78b90>
                  

                  虽然可以,但是因为有命名空间{http://www.test.com}",所以在每个标签前面加命名空间很不方便.

                  Although it can works, because there is a namespace "{http://www.test.com}", it's very inconvenient to add a namespace in front of each tag.

                  使用find"、findall"等方法时如何忽略命名空间?

                  How can I ignore the namespace when using the method of "find", "findall" and so on?

                  推荐答案

                  最好先解析XML文档,然后再修改结果中的标签,而不是修改XML文档本身.这样你就可以处理多个命名空间和命名空间别名:

                  Instead of modifying the XML document itself, it's best to parse it and then modify the tags in the result. This way you can handle multiple namespaces and namespace aliases:

                  from io import StringIO  # for Python 2 import from StringIO instead
                  import xml.etree.ElementTree as ET
                  
                  # instead of ET.fromstring(xml)
                  it = ET.iterparse(StringIO(xml))
                  for _, el in it:
                      prefix, has_namespace, postfix = el.tag.partition('}')
                      if has_namespace:
                          el.tag = postfix  # strip all namespaces
                  root = it.root
                  

                  这是基于这里的讨论:http://bugs.python.org/issue18304

                  更新: rpartition 而不是 partition 确保您在 postfix 中获得标签名称,即使有没有命名空间.因此,您可以将其浓缩:

                  Update: rpartition instead of partition makes sure you get the tag name in postfix even if there is no namespace. Thus you could condense it:

                  for _, el in it:
                      _, _, el.tag = el.tag.rpartition('}') # strip ns
                  

                  这篇关于Python ElementTree 模块:使用“find"、“findall"方法时如何忽略 XML 文件的命名空间来定位匹配元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:“import *"到底是什么?进口? 下一篇:类型对象“datetime.datetime"没有属性“datetime

                  相关文章

                  最新文章

                    <i id='eQCdT'><tr id='eQCdT'><dt id='eQCdT'><q id='eQCdT'><span id='eQCdT'><b id='eQCdT'><form id='eQCdT'><ins id='eQCdT'></ins><ul id='eQCdT'></ul><sub id='eQCdT'></sub></form><legend id='eQCdT'></legend><bdo id='eQCdT'><pre id='eQCdT'><center id='eQCdT'></center></pre></bdo></b><th id='eQCdT'></th></span></q></dt></tr></i><div id='eQCdT'><tfoot id='eQCdT'></tfoot><dl id='eQCdT'><fieldset id='eQCdT'></fieldset></dl></div>

                      <bdo id='eQCdT'></bdo><ul id='eQCdT'></ul>
                    <tfoot id='eQCdT'></tfoot>
                    1. <small id='eQCdT'></small><noframes id='eQCdT'>

                    2. <legend id='eQCdT'><style id='eQCdT'><dir id='eQCdT'><q id='eQCdT'></q></dir></style></legend>