the tostring and XML methods in ElementTree

mirandacascade at yahoo.com mirandacascade at yahoo.com
Sun May 7 16:55:07 EDT 2006


O/S: Windows XP Home
Vsn of Python: 2.4

Copy/paste of interactive window is immediately below; the
text/questions toward the bottom of this post will refer to the content
of the copy/paste

>>> from elementtree import ElementTree
>>> beforeRoot = ElementTree.Element('beforeRoot')
>>> beforeCtag = ElementTree.SubElement(beforeRoot, 'C')
>>> beforeCtag.text = 'I\x92m confused'
>>> type(beforeCtag.text)
<type 'str'>
>>> print beforeCtag.text
I'm confused
>>> resultToStr = ElementTree.tostring(beforeRoot)
>>> resultToStr
'<beforeRoot><C>I’m confused</C></beforeRoot>'
>>> afterRoot = ElementTree.XML(resultToStr)
>>> afterCtag = afterRoot[0]
>>> type(afterCtag.text)
<type 'unicode'>
>>> print afterCtag.text
I?m confused
>>>

I wanted to see what would happen if one used the results of a tostring
method as input into the XML method.  What I observed is this:
a) beforeCtag.text is of type <type 'str'>
b) beforeCtag.text when printed displays: I'm confused
c) afterCtag.text is of type <type 'unicode'>
d) afterCtag.text when printed displays: I?m confused

Question 1: assuming the following:
 a) beforeCtag.text gets assigned a value of 'I\x92m confused'
 b) afterRoot is built using the XML() method where the input to the
XML() method is the results of a tostring() method from beforeRoot
Are there any settings/arguments that could have been modified that
would have resulted in afterCtag.text being of type <type 'str'> and
afterCtag.text when printed displays:
 I'm confused

?

Another snippet from interactive window

>>> resultToStr2 = ElementTree.tostring(beforeRoot, encoding="utf-8")
>>> resultToStr2
'<beforeRoot><C>I’m confused</C></beforeRoot>'
>>> if resultToStr == resultToStr2:
... 	print 'equal'
...
equal
>>>

If I'm reading the signature of the tostring method in ElementTree.py
correctly, it looks like encoding gets assigned a value of None if the
tostring method gets called without a 2nd argument.  In the specific
examples above, the result of the tostring method was the same when an
encoding of utf-8 was specified as it was when no encoding was
specified.

Question 2: Does the fact that resultToStr is equal to resultToStr2
mean that an encoding of utf-8 is the defacto default when no encoding
is passed as an argument to the tostring method, or does it only mean
that in this particular example, they happened to be the same?

Another snippet

>>> fileHandle = open('c:/output1.text', 'w')
>>> fileHandle.write(beforeCtag.text)
>>> fileHandle.write(afterCtag.text)
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode character u'\x92' in
position 1: ordinal not in range(128)
>>> encodedCtagtext = afterCtag.text.encode("utf-8")
>>> type(encodedCtagtext)
<type 'str'>
>>> encodedCtagtext
'I\xc2\x92m confused'
>>> print encodedCtagtext
IÂ'm confused
>>> fileHandle.write(encodedCtagtext)
>>> ord(encodedCtagtext[1])
194
>>>

In this snippet, I am trying to discern what can be written  to a file
without raising an exception.  The variable beforeCtag.text can be
written, but an exception is raised when an attempt is made to write
the unicode variable afterCtag.text to the file.  The statement

encodedCtagtext = afterCtag.text.encode("utf-8")

was a shot-in-the-dark attempt to transform afterCtag.text to something
that can be written to a file without raising an exception.  What I
observed is that:
a) encodedCtagtext can be written to a file without raising an
exception
b) the second character in encodedCtagtext has an ordinal value of 194

Questions 3 and 4:
3) would it be possible to construct a statement of the form

newResult = afterCtag.text.encode(?? some argument ??)

where newResult was the same as beforeCtag.text?  If so, what should
the argument be to the encode method?

4) what is the second character in encodedCtagtext (the character with
an ordinal value of 194)?




More information about the Python-list mailing list