python - parse xml file and output to text file -
trying parse xml file (config.xml) elementtree , output text file. looked @ other similar ques here none helped me. using python 2.7.9
import xml.etree.elementtree et tree = et.parse('config.xml') notags = et.tostring(tree,encoding='us-ascii',method='text') print(notags)
output
traceback (most recent call last): file "./python_element", line 9, in <module> notags = et.tostring(tree,encoding='us-ascii',method='text') file "/usr/lib64/python2.7/xml/etree/elementtree.py", line 1126, in tostring elementtree(element).write(file, encoding, method=method file "/usr/lib64/python2.7/xml/etree/elementtree.py", line 814, in write _serialize_text(write, self._root, encoding) file "/usr/lib64/python2.7/xml/etree/elementtree.py", line 1005, in _serialize_text part in elem.itertext(): attributeerror: > 'elementtree' object has no attribute 'itertext'
instead of tree
(elementtree
object), pass element
object. can root element using .getroot()
method:
notags = et.tostring(tree.getroot(), encoding='utf-8',method='text')
Comments
Post a Comment