2

I'm trying to write an XML file using Python's ElementTree package. Basically I make a root element called allDepts, and then in each iteration of my for loop I call a function that returns a deptElement containing a bunch of information about a university department. I add every deptElement to allDepts, make an ElementTree out of allDepts, and try to write it to a file.

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)

For some reason, at the tree.write(f) line, I get this error: "TypeError: cannot concatenate 'str' and 'instance' objects". Each deptElement prints out fine in the for loop, making me think that getDeptElement() is working fine. I never get my "ERROR" message printed out. Does anyone know what I'm doing wrong?

EDIT: Here's the full stack trace:

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))

1 Answer 1

2

Seem following line is cause.

    print "ERROR: " + dept

Change as follow and retry:

    print "ERROR: ", dept

OR

    print "ERROR: " + str(dept)

ADD

Second argument to ET.SubElement should be str. Is deptElement is str? If deptElement is Element, use 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

ADD 2 To reproduce error (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

To reproduce error (Python 2.7, different error message):

>>> 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)
Sign up to request clarification or add additional context in comments.

3 Comments

Tried that and it didn't help - dept is already a string and this line is never executed anyway. I've added the stack trace in my question, which shows that it's the tree.write(f) line that's causing the error.
@FrancesKR, added another possible causes.
Thank you, it was append() rather than SubElement()!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.