Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
删除Python 3 lxml中的所有注释_Python_Python 3.x_Xpath_Lxml - Fatal编程技术网

删除Python 3 lxml中的所有注释

删除Python 3 lxml中的所有注释,python,python-3.x,xpath,lxml,Python,Python 3.x,Xpath,Lxml,我有一个XML文件,以前我对一些元素进行了注释,现在我想取消注释它们 我有这个结构 <parent parId="22" attr="Alpha"> <!--<reg regId="1"> <cont>There is some content</cont><cont2 attr1="val">Another content</cont2> </reg> --></parent>

我有一个XML文件,以前我对一些元素进行了注释,现在我想取消注释它们

我有这个结构

<parent parId="22" attr="Alpha">
 <!--<reg regId="1">
  <cont>There is some content</cont><cont2 attr1="val">Another content</cont2>
 </reg>
--></parent>
<parent parId="23" attr="Alpha">
 <reg regId="1">
  <cont>There is more content</cont><cont2 attr1="noval">Morecont</cont2>
 </reg>
</parent>
<parent parId="24" attr="Alpha">
 <!--<reg regId="1">
  <cont>There is some content</cont><cont2 attr1="val">Another content</cont2>
 </reg>
--></parent>
但是,替换未按预期的方式工作


我怎样才能解决这个问题

既然您想取消注释所有内容,那么您真正需要做的就是删除每个“<!--”和“->”:

重新导入
new_xml=''.join(关于拆分('',xml))
或:

new_xml=xml.replace(“”,“”)

既然您想取消注释所有内容,那么您真正需要做的就是删除每个“<!--”和“->”:

重新导入
new_xml=''.join(关于拆分('',xml))
或:

new_xml=xml.replace(“”,“”)

您的代码缺少从注释文本创建新XML元素的关键部分。还有一些其他错误与不正确的XPath查询以及在循环中多次保存输出文件有关

此外,您似乎正在将
xml.etree
lxml.etree
混合使用。根据,前者在解析XML文件时忽略注释,因此最好的方法是使用

在解决了以上所有问题后,我们得到了类似的结果

import lxml.etree as ET


def unhide_element():
    path = r'test.xml'
    root = ET.parse(path)
    comments = root.xpath('//comment()')
    for c in comments:
        print('Comment: ', c)
        parent_comment = c.getparent()
        parent_comment.remove(c)  # skip this if you want to retain the comment
        new_elem = ET.XML(c.text)  # this bit creates the new element from comment text
        parent_comment.addnext(new_elem)

    root.write(r'new_file.xml')

您的代码缺少从注释文本创建新XML元素的关键部分。还有一些其他错误与不正确的XPath查询以及在循环中多次保存输出文件有关

此外,您似乎正在将
xml.etree
lxml.etree
混合使用。根据,前者在解析XML文件时忽略注释,因此最好的方法是使用

在解决了以上所有问题后,我们得到了类似的结果

import lxml.etree as ET


def unhide_element():
    path = r'test.xml'
    root = ET.parse(path)
    comments = root.xpath('//comment()')
    for c in comments:
        print('Comment: ', c)
        parent_comment = c.getparent()
        parent_comment.remove(c)  # skip this if you want to retain the comment
        new_elem = ET.XML(c.text)  # this bit creates the new element from comment text
        parent_comment.addnext(new_elem)

    root.write(r'new_file.xml')

太好了,这奏效了。但是,我不知道为什么我的lxml版本在没有先使用getroot()的情况下不能工作。我不能直接在ElementTree中使用parse。太好了,这很有效。但是,我不知道为什么我的lxml版本在没有先使用getroot()的情况下不能工作。我无法直接在ElementTree中使用parse。
new_xml = xml.replace('<!--', '').replace('-->', '')
import lxml.etree as ET


def unhide_element():
    path = r'test.xml'
    root = ET.parse(path)
    comments = root.xpath('//comment()')
    for c in comments:
        print('Comment: ', c)
        parent_comment = c.getparent()
        parent_comment.remove(c)  # skip this if you want to retain the comment
        new_elem = ET.XML(c.text)  # this bit creates the new element from comment text
        parent_comment.addnext(new_elem)

    root.write(r'new_file.xml')