I have this problem, AttributeError: type object 'ElementTree' has no attribute 'tostring' , I donβt know what is wrong, I import into a string, this does not work. Try another tutorial, but nothing.
Is there any other way to convert an ElementTree object to an XML string?
import os import re import glob from xml.dom import minidom from time import strftime from xml.etree.ElementTree import ElementTree from xml.etree.ElementTree import Element, SubElement, Comment, tostring date=strftime("%Y-%m-%dT%H:%M:%S") os.chdir('/home/guillermo/TclsPy/XML_Level2/') def prettify(elem): """Return a pretty-printed XML string for the Element. """ rough_string = ElementTree.tostring(elem, 'utf-8',method="xml" , short_empty_elements=True) reparsed = minidom.parseString(rough_string) return reparsed.toprettyxml(indent=" ") for nameXml in glob.glob("*.xml"): new_nameXml = re.sub(r'tlmy',r'AUX',nameXml,flags=re.IGNORECASE) new_nameXml = re.sub('.xml','_AlarmEnabled.xml',new_nameXml, flags=re.IGNORECASE)
Guillermo
source share