• <legend id='XGZoY'><style id='XGZoY'><dir id='XGZoY'><q id='XGZoY'></q></dir></style></legend>

    <tfoot id='XGZoY'></tfoot>

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

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

      1. 使用 lxml 从 python 中的 xml 中删除命名空间和前缀

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

          • <small id='FSByy'></small><noframes id='FSByy'>

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

                <tbody id='FSByy'></tbody>

                <legend id='FSByy'><style id='FSByy'><dir id='FSByy'><q id='FSByy'></q></dir></style></legend>

                  <tfoot id='FSByy'></tfoot>
                  本文介绍了使用 lxml 从 python 中的 xml 中删除命名空间和前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我需要打开一个 xml 文件并对其进行一些更改,其中一项更改是删除名称空间和前缀,然后保存到另一个文件.这是xml:

                  I have an xml file I need to open and make some changes to, one of those changes is to remove the namespace and prefix and then save to another file. Here is the xml:

                  <?xml version='1.0' encoding='UTF-8'?>
                  <package xmlns="http://apple.com/itunes/importer">
                    <provider>some data</provider>
                    <language>en-GB</language>
                  </package>
                  

                  我可以进行我需要的其他更改,但不知道如何删除命名空间和前缀.这是我需要的 reusklt xml:

                  I can make the other changes I need, but can't find out how to remove the namespace and prefix. This is the reusklt xml I need:

                  <?xml version='1.0' encoding='UTF-8'?>
                  <package>
                    <provider>some data</provider>
                    <language>en-GB</language>
                  </package>
                  

                  这是我的脚本,它将打开并解析 xml 并保存它:

                  And here is my script which will open and parse the xml and save it:

                  metadata = '/Users/user1/Desktop/Python/metadata.xml'
                  from lxml import etree
                  parser = etree.XMLParser(remove_blank_text=True)
                  open(metadata)
                  tree = etree.parse(metadata, parser)
                  root = tree.getroot()
                  tree.write('/Users/user1/Desktop/Python/done.xml', pretty_print = True, xml_declaration = True, encoding = 'UTF-8')
                  

                  那么我将如何在脚本中添加代码来删除命名空间和前缀?

                  So how would I add code in my script which will remove the namespace and prefix?

                  推荐答案

                  按照 Uku Loskit 的建议替换标签.除此之外,使用 lxml.objectify.deannotate.

                  Replace tag as Uku Loskit suggests. In addition to that, use lxml.objectify.deannotate.

                  from lxml import etree, objectify
                  
                  metadata = '/Users/user1/Desktop/Python/metadata.xml'
                  parser = etree.XMLParser(remove_blank_text=True)
                  tree = etree.parse(metadata, parser)
                  root = tree.getroot()
                  
                  ####    
                  for elem in root.getiterator():
                      if not hasattr(elem.tag, 'find'): continue  # (1)
                      i = elem.tag.find('}')
                      if i >= 0:
                          elem.tag = elem.tag[i+1:]
                  objectify.deannotate(root, cleanup_namespaces=True)
                  ####
                  
                  tree.write('/Users/user1/Desktop/Python/done.xml',
                             pretty_print=True, xml_declaration=True, encoding='UTF-8')
                  

                  更新

                  Comment 等一些标签在访问 tag 属性时会返回一个函数.为此增加了一名警卫.(1)

                  Some tags like Comment return a function when accessing tag attribute. added a guard for that. (1)

                  这篇关于使用 lxml 从 python 中的 xml 中删除命名空间和前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:类型对象“datetime.datetime"没有属性“datetime 下一篇:Python:NameError:未定义全局名称“foobar"

                  相关文章

                  最新文章

                1. <tfoot id='8a2yz'></tfoot>

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

                  <legend id='8a2yz'><style id='8a2yz'><dir id='8a2yz'><q id='8a2yz'></q></dir></style></legend>

                  1. <small id='8a2yz'></small><noframes id='8a2yz'>