Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将PythonElementTree写入文件时抛出TypeError_Python_Xml_Typeerror_Elementtree - Fatal编程技术网

将PythonElementTree写入文件时抛出TypeError

将PythonElementTree写入文件时抛出TypeError,python,xml,typeerror,elementtree,Python,Xml,Typeerror,Elementtree,我正在尝试使用Python的ElementTree包编写一个XML文件。基本上,我创建了一个名为allDepts的根元素,然后在for循环的每次迭代中调用一个函数,该函数返回一个deptElement,其中包含一组关于大学系的信息。我将每个deptement添加到allDepts,从allDepts中创建一个ElementTree,并尝试将其写入文件 def crawl(year, season, campus): departments = getAllDepartments(year,

我正在尝试使用Python的ElementTree包编写一个XML文件。基本上,我创建了一个名为
allDepts
的根元素,然后在for循环的每次迭代中调用一个函数,该函数返回一个
deptElement
,其中包含一组关于大学系的信息。我将每个
deptement
添加到
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)
出于某种原因,在
树.write(f)
行,我得到了这个错误:“TypeError:无法连接'str'和'instance'对象”。每个
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))
文件“/CourseInfo.py”,第210行,在
爬行(“2013”、“S”、“UBC”)
文件“/courseinfo-crawl.py”,第207行,在爬网中
tree.write(f)
文件“/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/xml/etree/ElementTree.py”,第663行,以书面形式
self.\u write(文件,self.\u根,编码,{})
文件“/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/xml/etree/ElementTree.py”,第707行,在
self._write(文件、n、编码、名称空间)
文件“/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/xml/etree/ElementTree.py”,第681行,在

file.write(“似乎以下行是原因

    print "ERROR: " + dept
更改如下,然后重试:

    print "ERROR: ", dept

添加

ET.SubElement的第二个参数应该是str。Is
deptElement
Is str? 如果
deptement
是元素,则使用
allDepts.append(deptement)

添加2 要再现错误(Python 2.6),请执行以下操作:

>从xml.etree导入元素树作为ET
>>>allDepts=ET.Element('depts')
>>>ET.SubElement(所有部门,ET.Element('a'))
>>>以open('a','wb')作为f:
…tree=ET.ElementTree(所有部门)
…树。写入(f)
...
回溯(最近一次呼叫最后一次):
文件“”,第3行,在
文件“/home/falsetru/t/Python-2.6/Lib/xml/etree/ElementTree.py”,第663行,写入
self.\u write(文件,self.\u根,编码,{})
文件“/home/falsetru/t/Python-2.6/Lib/xml/etree/ElementTree.py”,第707行,在_write中
self._write(文件、n、编码、名称空间)
文件“/home/falsetru/t/Python-2.6/Lib/xml/etree/ElementTree.py”,第681行,以书面形式

file.write(“尝试过,但没有帮助-
dept
已经是一个字符串,而且这一行永远不会执行。我在问题中添加了堆栈跟踪,这表明是
树。write(f)
行导致了错误。@FrancesKR,添加了另一个可能的原因。谢谢,这是
append()
而不是
子元素()
    print "ERROR: " + str(dept)
>>> 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
>>> 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)