【发布时间】:2016-06-28 13:16:51
【问题描述】:
我想从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', 'Comment', 'CommentBase', 'CustomElementClassLookup', 'DEBUG', 'DTD', 'DTDError', 'DTDParseError', 'DTDValidateError ','DocInfo','DocumentInvalid','ETCompatXMLParser','ETXPath','Element','ElementBase','ElementChildIterator','ElementClassLookup','ElementDefaultClassLookup','ElementDepthFirstIterator','ElementNamespaceClassLookup','ElementTextIterator', 'ElementTree'、'Entity'、'EntityBase'、'Error'、'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'、'Re laxNGError”、“RelaxNGErrorTypes”、“RelaxNGParseError”、“RelaxNGValidateError”、“解析器”、“Schematron”、“SchematronError”、“SchematronParseError”、“SchematronValidateError”、“SerialisationError”、“SiblingsIterator”、“SubElement”、“TreeBuilder” ,'XInclude','XIncludeError','XML','XMLDTDID','XMLID','XMLParser','XMLPullParser','XMLSchema','XMLSchemaError','XMLSchemaParseError','XMLSchemaValidateError','XMLSyntaxError',' XMLTreeBuilder','XPath','XPathDocumentEvaluator','XPathElementEvaluator','XPathError','XPathEvalError','XPathEvaluator','XPathFunctionError','XPathResultError','XPathSyntaxError','XSLT','XSLTAccessControl','XSLTApplyError' ,“XSLTError”,“XSLTExtension”,“XSLTExtensionError”,“XSLTParseError”,“XSLTSaveError”,“_Attrib”,“_BaseErrorLog”,“_Comment”,“_Document”,“_DomainErrorLog”,“_Element”,“_ElementIterator”,“ _ElementMatchIterator'、'_ElementStringResult'、'_ElementTagMatcher'、'_ElementTree'、'_ElementUnicodeResult'、'_Entity'、'_ErrorLog'、'_Fee dParser','_IDDict','_ListErrorLog','_LogEntry','_ProcessingInstruction','_RotatingErrorLog','_SaxParserTarget','_TargetParserResult','_Validator','_XPathEvaluatorBase','_XSLTProcessingInstruction','_XSLTResultTree','all', 'builtins', 'doc', 'docformat', 'file ', 'name', 'package', 'pyx_capi', 'test', '版本'、'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','strip_elements','strip_tags','tostring','tostringlist','tounicode','use_global_python_log',' xml文件']
【问题讨论】:
-
如何导入模块?你能发布
dir(root)的结果吗?
标签: python python-2.7 elementtree