AttributeError: objet de type 'ElementTree' n'a pas d'attribut "tostring'
J'ai ce problème , AttributeError: type object 'ElementTree' has no attribute 'tostring'
, je ne sais pas ce qui est faux, je l'importation de chaîne, Il ne fonctionne pas.
Essayez de suivre un autre tuto mais, rien.
Est-il un autre moyen de convertir un ElementTree objet dans une chaîne XML?
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)#new_nameXml.lower().replace('.xml','_AlarmEnabled.xml')
match = re.search(r'TSL_TM_TLMY_(.+).xml', nameXml,flags= re.IGNORECASE )
if match:
#Si matchea el patron, guardamos en subsistema lo que esta entre ()
subsistema = match.group(1)
print "Nombre:", new_nameXml
print "Subsistema:", subsistema
else:
print "No matchea:", nameXml
# raw_input()
#<?xml version="1.0" encoding="UTF-8"?>
TSLFunction=Element('TSLFunction')
child = SubElement(TSLFunction, 'unit')
child.text = '<![CDATA[AUX]]>'
child1= SubElement(TSLFunction,'mnemonic')
child1.text='<![CDATA'+'['+'AUX::'+'AUX.'+subsistema.replace('.xml','')+'_AlarmEnabled]]'
child2=SubElement(TSLFunction, 'body')
child2.text='<![CDATA[out = true;]]>'
child3=SubElement(TSLFunction,'description')
child3.text='<![CDATA[--]]>'
child4=SubElement(TSLFunction,'name')
child4.text='<![CDATA['+subsistema+'_AlarmEnabled'+']]'
child5=SubElement(TSLFunction,'minorVersion')
child5.text='0'
child6=SubElement(TSLFunction,'majorVersion')
child6.text='1'
child7=SubElement(TSLFunction,'lastUpdate')
child7.text=date
child8=SubElement(TSLFunction,'creationDate')
child8.text=date
child9=SubElement(TSLFunction,'checked')
child9.text='false'
returnchild=SubElement(TSLFunction,'return')
name=SubElement(returnchild,'name')
name.text='<![CDATA[out]]>'
returnType=SubElement(returnchild,'returnType')
returnType.text='boolean'
label=SubElement(returnchild,'label')
label.text='<![CDATA[--]]>'
parameters=SubElement(TSLFunction,'parameters')
subtype=SubElement(TSLFunction,'subtype')
subtype.text='<![CDATA[TM]]>'
prefix=SubElement(TSLFunction,'prefix')
prefix.text='<![CDATA[AUX]]>'
variable=SubElement(TSLFunction,'variable')
variable.text='<![CDATA['+subsistema +'_AlarmEnabled]]'
print (subsistema)
tree = ElementTree(prettify(TSLFunction))
tree.write('../Alarm/'+new_nameXml)enter code here
OriginalL'auteur Guillermo | 2013-09-20
Vous devez vous connecter pour publier un commentaire.
La
xml.etree.ElementTree.ElementTree
classe n'ont pas detostring
méthode. C'est une fonction dans lexml.etree.ElementTree
module.Vous avez importé
tostring
à partir du module déjà dans votre code. Changementà
et cela devrait fonctionner.
La
short_empty_elements
paramètre est uniquement disponible en Python 3.4. Il ne fonctionne pas dans Python 2.7.merci pour l'aide , j'ai l'aide de python 2.7.4
Je vois maintenant que les instructions d'impression sont Python 2 style. Alors, pourquoi est la question tagged [python-3.x]?
désolé j'ai confondu
J'ai remarqué un couple de problèmes déjà, mais je ne sais pas comment reproduire ce nouveau message d'erreur que vous mettez dans un commentaire. Un test complet de cas (voir la section sscce.org) serait plus facile de vous aider.
OriginalL'auteur mzjn
Vous importez la mauvaise chose.
tostring
est une fonction dans lexml.etree.ElementTree
module, pas une méthode dexml.etree.ElementTree.ElementTree
.OriginalL'auteur Wooble