如何删除etree python中的元素?

时间:2016-06-28 13:16:51

标签: python python-2.7 elementtree

我想从etree中删除一个元素,怎么做?这是代码

from lxml import etree
root = etree.parse(open("Sample.xml",'r'))
for e in root.iter():
  if(some condition):
     root.remove(e) 

上面的代码给了我 AttributeError:' lxml.etree._ElementTree'对象没有属性'删除'。 还有其他方法可以删除元素吗?

dir(etree)的结果

  
    
      

[' AncestorsIterator',' AttributeBasedElementClassLookup',' C14NError',' CDATA','评论',& #39; CommentBase',' CustomElementClassLookup'' DEBUG',' DTD',' DTDError',' DTDParseError&# 39;,' DTDValidateError',' DocInfo',' DocumentInvalid',' ETCompatXMLParser',' ETXPath',&# 39; Element',' ElementBase',' ElementChildIterator',' ElementClassLookup',' ElementDefaultClassLookup',' ElementDepthFirstIterator' ElementDepthFirstIterator' ;,' ElementNamespaceClassLookup',' ElementTextIterator',' ElementTree','实体',' EntityBase',' ;错误',' ErrorDomains' ErrorLevels',' ErrorTypes',' Extension',' FallbackElementClassLookup' ,' FunctionNamespace',' HTML',' HTMLParser',' HTMLPullParser',' LIBXML_COMPILED_VERSION',' LIBXML_VERSION' ,' LIBXSLT_COMPILED_VERSION',' LIBXSLT_VERSION',' LXML_VERSION',' LxmlError',' LxmlRegistryError',' LxmlSyntaxError',' NamespaceRegistryError',' PI',' PIBase',' ParseError',' ParserBasedElementClassLookup', ' ParserError',' ProcessingInstruction',' PyErrorLog',' PythonElementClassLookup',' QName',' RelaxNG& #39;,' RelaxNGError',' RelaxNGErrorTypes',' RelaxNGParseError',' RelaxNGValidateError',' Resolver',& #39; Schematron',' SchematronError',' SchematronParseError',' SchematronValidateError',' SerialisationError',' SiblingsIterator&# 39;,' SubElement',' TreeBuilder',' XInclude',' XIncludeError',' XML',&# 39; XMLDTDID',' XMLID',' XMLParser',' XMLPullParser',' XMLSchema',' XMLSchemaError' ;,' XMLSche maParseError',' XMLSchemaValidateError' XMLSyntaxError',' XMLTreeBuilder',' XPath',' XPathDocumentEvaluator', ' XPathElementEvaluator',' XPathError',' XPathEvalError',' XPathEvaluator',' XPathFunctionError',' XPathResultError& #39;,' XPathSyntaxError',' XSLT',' XSLTAccessControl',' XSLTApplyError',' XSLTError',& #39; XSLTExtension',' XSLTExtensionError',' XSLTParseError',' XSLTSaveError',' _Attrib',' _BaseErrorLog&# 39;,' _评论',' _文档',' _DomainErrorLog',' _Element',' _ElementIterator',&# 39; _ElementMatchIterator',' _ElementStringResult',' _ElementTagMatcher',' _ElementTree',' _ElementUnicodeResult',' _Entity&#39 ;,' _ErrorLog',' _FeedParser',' _IDDict',' _ListErrorLog',' _LogEntry',' ;_处理ingInstruction',' _RotatingErrorLog',' _SaxParserTarget',#39; _TargetParserResult',' _Validator',' _XPathEvaluatorBase', ' _XSLTProcessingInstruction',' _XSLTResultTree',' 所有',' 内置&#39 ;,' doc ',' docformat ',' 文件&# 39;,' 名称',' ',' pyx_capi & #39;,' 测试',' 版本',' cleanup_namespaces',' ; clear_error_log',' dump',' fromstring',' fromstringlist',' get_default_parser',' htmlfile' ,' iselement',' iterparse',' iterwalk',' memory_debugger','解析',' parseid',' register_namespace',' set_default_parser',' set_element_class_lookup',' strip_attributes',' s trip_elements',' strip_tags',' tostring',' tostringlist',' tounicode',' use_global_python_log', ' XMLFILE']

    
  

1 个答案:

答案 0 :(得分:1)

也许e.getparent().remove(e)

相关问题