Skip to main content
 首页 » 编程设计

python之将 Python ElementTree 写入文件会抛出 TypeError

2024年11月01日6arxive

我正在尝试使用 Python 的 ElementTree 包编写一个 XML 文件。基本上,我创建了一个名为 allDepts 的根元素,然后在我的 for 循环的每次迭代中,我调用一个函数,该函数返回一个 deptElement,其中包含有关大学部门的大量信息。我将每个 deptElement 添加到 allDepts,从 allDepts 中创建一个 ElementTree,然后尝试将其写入文件.

def crawl(year, season, campus): 
  departments = getAllDepartments(year, season, campus) 
  allDepts = ET.Element('depts') 
 
  for dept in departments: 
    deptElement = getDeptElement(allDepts, dept, year, season, campus) 
    print ET.tostring(deptElement)    #Prints fine here! 
    ET.SubElement(allDepts, deptElement) 
 
    if deptElement == None: 
        print "ERROR: " + dept 
 
  with open(str(year) + season + "_" + campus + "_courses.xml", 'w') as f: 
    tree = ET.ElementTree(allDepts) 
    tree.write(f) 

出于某种原因,在 tree.write(f) 行,我收到此错误:“TypeError: cannot concatenate 'str' and 'instance' objects”。每个 deptElement 在 for 循环中打印得很好,让我认为 getDeptElement() 工作正常。我从来没有打印出我的“错误”信息。有谁知道我做错了什么?

编辑:这是完整的堆栈跟踪:

File "./CourseInfoCrawl.py", line 210, in <module> 
crawl("2013", "S", "UBC") 
File "./CourseInfoCrawl.py", line 207, in crawl 
tree.write(f) 
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/xml/etree/ElementTree.py", line 663, in write 
self._write(file, self._root, encoding, {}) 
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/xml/etree/ElementTree.py", line 707, in _write 
self._write(file, n, encoding, namespaces) 
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/xml/etree/ElementTree.py", line 681, in _write 
file.write("<" + _encode(tag, encoding)) 

请您参考如下方法:

似乎下一行是原因。

    print "ERROR: " + dept 

如下更改并重试:

    print "ERROR: ", dept 

或

    print "ERROR: " + str(dept) 

添加

ET.SubElement 的第二个参数应该是 str。 deptElement 是 str 吗? 如果 deptElement 是 Element,使用 allDepts.append(deptElement)。

http://docs.python.org/2/library/xml.etree.elementtree.html#xml.etree.ElementTree.SubElement http://docs.python.org/2/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.append

添加 2 要重现错误(Python 2.6):

>>> from xml.etree import ElementTree as ET 
>>> allDepts = ET.Element('depts') 
>>> ET.SubElement(allDepts, ET.Element('a')) 
<Element <Element a at b727b96c> at b727b22c> 
>>> with open('a', 'wb') as f: 
...     tree = ET.ElementTree(allDepts) 
...     tree.write(f) 
... 
Traceback (most recent call last): 
  File "<stdin>", line 3, in <module> 
  File "/home/falsetru/t/Python-2.6/Lib/xml/etree/ElementTree.py", line 663, in write 
    self._write(file, self._root, encoding, {}) 
  File "/home/falsetru/t/Python-2.6/Lib/xml/etree/ElementTree.py", line 707, in _write 
    self._write(file, n, encoding, namespaces) 
  File "/home/falsetru/t/Python-2.6/Lib/xml/etree/ElementTree.py", line 681, in _write 
    file.write("<" + _encode(tag, encoding)) 
TypeError: cannot concatenate 'str' and 'instance' objects 

重现错误(Python 2.7,不同的错误消息):

>>> from xml.etree import ElementTree as ET 
>>> allDepts = ET.Element('depts') 
>>> ET.SubElement(allDepts, ET.Element('a')) 
<Element <Element 'a' at 0xb745a8ec> at 0xb74601ac> 
>>> with open('a', 'wb') as f: 
...     tree = ET.ElementTree(allDepts) 
...     tree.write(f) 
... 
Traceback (most recent call last): 
  File "<stdin>", line 3, in <module> 
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 817, in write 
    self._root, encoding, default_namespace 
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 886, in _namespaces 
    _raise_serialization_error(tag) 
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1052, in _raise_serialization_error 
    "cannot serialize %r (type %s)" % (text, type(text).__name__) 
TypeError: cannot serialize <Element 'a' at 0xb745a8ec> (type Element)