【问题标题】:AttributeError: type object 'ElementTree' has no attribute 'tostring'AttributeError:类型对象'ElementTree'没有属性'tostring'
【发布时间】:2013-09-25 23:51:07
【问题描述】:

我有这个问题,AttributeError: type object 'ElementTree' has no attribute 'tostring',我不知道怎么了,我导入到字符串,它不起作用。 尝试遵循另一个教程,但没有。

还有其他方法可以将 ElementTree 对象转换为 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

【问题讨论】:

    标签: xml python-2.7 tostring elementtree


    【解决方案1】:

    xml.etree.ElementTree.ElementTree 类没有tostring 方法。它是xml.etree.ElementTree 模块中的一个函数。

    您已经从代码中的模块导入了tostring。改变

    rough_string = ElementTree.tostring(elem, 'utf-8', method="xml", short_empty_elements=True)
    

    rough_string = tostring(elem, 'utf-8', method="xml")
    

    它应该可以工作。

    short_empty_elements 参数仅在 Python 3.4 中可用。它不适用于 Python 2.7。

    【讨论】:

    • 几乎工作你有这个错误:回溯(最近一次调用最后一次):文件“/home/guillermo/TclsPy/Match.py​​”,第 92 行,在 tree.write('. ./Alarm/'+new_nameXml) 文件“/usr/lib/python2.7/xml/etree/ElementTree.py”,第 817 行,写入 self._root,编码,default_namespace 文件“/usr/lib/python2.7 /xml/etree/ElementTree.py",第 876 行,在 _namespaces 中 iterate = elem.getiterator # cET 兼容性 AttributeError: 'unicode' object has no attribute 'getiterator'
    • 我现在看到打印语句是 Python 2 风格的。那么为什么这个问题被标记为 [python-3.x]?
    • 我已经指出了几个问题,但我不知道如何重现您在评论中提出的这个新错误。一个完整的测试用例(参见sscce.org)会更容易提供帮助。
    【解决方案2】:

    您正在导入错误的内容。 tostringxml.etree.ElementTree 模块中的函数,而不是xml.etree.ElementTree.ElementTree 的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-05
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 2019-06-08
      相关资源
      最近更新 更多