473,406 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

iterparse and unicode

It seems xml.etree.cElementTree.iterparse() is not unicode aware:
>>from StringIO import StringIO
from xml.etree.cElementTree import iterparse
s = u'<name>\u03a0\u03b1\u03bd\u03b1\u03b3\u03b9\u03ce \u03c4\u03b7\u03c2</name>'
for event,elem in iterparse(StringIO(s)):
.... print elem.text
....
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 64, in __iter__
UnicodeEncodeError: 'ascii' codec can't encode characters in position
6-15: ordinal not in range(128)

Am I using it incorrectly or it doesn't currently support unicode ?

George
Aug 20 '08 #1
13 5022
On Aug 21, 8:36 am, George Sakkis <george.sak...@gmail.comwrote:
It seems xml.etree.cElementTree.iterparse() is not unicode aware:
>from StringIO import StringIO
from xml.etree.cElementTree import iterparse
s = u'<name>\u03a0\u03b1\u03bd\u03b1\u03b3\u03b9\u03ce \u03c4\u03b7\u03c2</name>'
for event,elem in iterparse(StringIO(s)):

... print elem.text
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 64, in __iter__
UnicodeEncodeError: 'ascii' codec can't encode characters in position
6-15: ordinal not in range(128)

Am I using it incorrectly or it doesn't currently support unicode ?
Hi George,
I'm no XML guru by any means but as far as I understand it, you would
need to encode your text into UTF-8, and prepend something like '<?xml
version="1.0" encoding="UTF-8" standalone="yes"?>' to it. This appears
to be the way XML is, rather than an ElementTree problem.

E.g.
>>from StringIO import StringIO
from xml.etree.cElementTree import iterparse
s = u'<wrapper><name>\u03a0\u03b1</name><digits>01234567</digits></wrapper>'
>>h = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
xml = h + s.encode('utf8')
for event,elem in iterparse(StringIO(xml)):
.... print elem.tag, repr(elem.text)
....
name u'\u03a0\u03b1'
digits '01234567'
wrapper None
>>>
HTH,
John
Aug 20 '08 #2
On Wed, 2008-08-20 at 15:36 -0700, George Sakkis wrote:
It seems xml.etree.cElementTree.iterparse() is not unicode aware:
>from StringIO import StringIO
from xml.etree.cElementTree import iterparse
s = u'<name>\u03a0\u03b1\u03bd\u03b1\u03b3\u03b9\u03ce \u03c4\u03b7\u03c2</name>'
for event,elem in iterparse(StringIO(s)):
... print elem.text
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 64, in __iter__
UnicodeEncodeError: 'ascii' codec can't encode characters in position
6-15: ordinal not in range(128)

Am I using it incorrectly or it doesn't currently support unicode ?

George
--
http://mail.python.org/mailman/listinfo/python-list
As iterparse expects an actual file as input, using a unicode string is
problematic. If you want to use iterparse, the simplest way would be to
encode your string before inserting it into the StringIO object, as so:

>>for event,elem in iterparse(StringIO(s.encode('UTF8')):
.... print elem.text
....

If you encode using UTF-8, you don't need to worry about the <?xml header
bit as suggested previously, as it's the default for XML.

If you're using unicode extensively, you should consider using lxml,
which implements the same interface as ElementTree, but handles unicode
better (though it also doesn't run your example above without first
encoding the string):
http://codespeak.net/lxml/parsing.ht...nicode-strings

You may also find the target parser interface to be more accepting of
unicode than iterparse, though it requires a different parsing interface:
http://codespeak.net/lxml/parsing.ht...rser-interface

--
John Krukoff <jk******@ltgc.com>
Land Title Guarantee Company

Aug 21 '08 #3
Thank you both for the suggestions. I made a few more experiments to
understand how iterparse behaves with respect to three dimensions:

a. Is the encoding declared in the header (if there is one) ?
b. Is the text ascii-encodable (i.e. within range(128)) ?
c. Does the passed file object's read() method return str or unicode
(e.g. codecs.open(f,encoding='utf8')) ?

Feel free to correct me if I misinterpreted what is really happening.

As John Krukoff mentioned, omitting the encoding is equivalent to
encoding="utf-8" for all other combinations. This leaves (b) and (c).

If a text node is ascii-encodable, iterparse() returns it as a byte
string, regardless of the declared encoding and the input file's
read() return type.

(c) becomes relevant only if a text node is not ascii-encodable. In
this case iterparse() returns unicode if the underlying file's read()
returns bytes in an encoding that matches (or at least is compatible
with) the declared encoding in the header (or the implied utf8).
Passing a file object whose read() returns unicode characters
implicitly encodes them to ascii, which raises a UnicodeEncodeError
since the text node is not ascii-encodable.

It's interesting that the element text attributes after a successful
parse do not necessarily have the same type, i.e. all be str or all
unicode. I ported some text extraction code from BeautifulSoup (which
handles all text as unicode) and I was surprized to find out that in
xml.etree the returned text's type is not fixed, even within the same
file. Although it's not a bug, having a mixed collection of byte and
unicode strings from the same source makes me somewhat uneasy.

George
Aug 21 '08 #4
George Sakkis wrote:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 64, in __iter__
UnicodeEncodeError: 'ascii' codec can't encode characters in position
6-15: ordinal not in range(128)

Am I using it incorrectly or it doesn't currently support unicode ?
iterparse parses XML documents. XML documents are streams of encoded
characters, not streams of Unicode characters.

</F>

Aug 21 '08 #5
George Sakkis wrote:
Thank you both for the suggestions. I made a few more experiments to
understand how iterparse behaves with respect to three dimensions:
Spending time researching undefined behaviour is pretty pointless. ET
parsers expect byte streams, because that's what XML files are. If you
pass it anything else, an ET implementation may attempt to convert that
thing to a byte string, run the game "rogue", or do something else that
it finds appropriate.
It's interesting that the element text attributes after a successful
parse do not necessarily have the same type, i.e. all be str or all
unicode. I ported some text extraction code from BeautifulSoup (which
handles all text as unicode) and I was surprized to find out that in
xml.etree the returned text's type is not fixed, even within the same
file. Although it's not a bug, having a mixed collection of byte and
unicode strings from the same source makes me somewhat uneasy.
If you don't care about memory and execution performance, there are
plenty of toolkits that guarantee that you always get Unicode strings.

</F>

Aug 21 '08 #6
On Aug 21, 1:48*am, Fredrik Lundh <fred...@pythonware.comwrote:
George Sakkis wrote:
It's interesting that the element text attributes after a successful
parse do not necessarily have the same type, i.e. all be str or all
unicode. I ported some text extraction code from *BeautifulSoup (which
handles all text as unicode) and I was surprized to find out that in
xml.etree the returned text's type is not fixed, even within the same
file. Although it's not a bug, having a mixed collection of byte and
unicode strings from the same source makes me somewhat uneasy.

If you don't care about memory and execution performance, there are
plenty of toolkits that guarantee that you always get Unicode strings.
As long as they are documented, both approaches are fine for different
cases. Currently the only reference I found about unicode in
ElementTree is "All strings can either be Unicode strings, or 8-bit
strings containing US-ASCII only." [1], which is rather ambiguous; at
least I read it as "all strings are Unicode or all strings are 8-bit
strings", not a potentially mix of both in the same tree.

Regards,
George

[1] http://effbot.org/zone/element.htm
Aug 21 '08 #7
George Sakkis wrote:
On Aug 21, 1:48 am, Fredrik Lundh <fred...@pythonware.comwrote:
>George Sakkis wrote:
>>It's interesting that the element text attributes after a successful
parse do not necessarily have the same type, i.e. all be str or all
unicode. I ported some text extraction code from BeautifulSoup (which
handles all text as unicode) and I was surprized to find out that in
xml.etree the returned text's type is not fixed, even within the same
file. Although it's not a bug, having a mixed collection of byte and
unicode strings from the same source makes me somewhat uneasy.
If you don't care about memory and execution performance, there are
plenty of toolkits that guarantee that you always get Unicode strings.

As long as they are documented, both approaches are fine for different
cases. Currently the only reference I found about unicode in
ElementTree is "All strings can either be Unicode strings, or 8-bit
strings containing US-ASCII only." [1], which is rather ambiguous
It's not ambiguous in Py2.x, where ASCII byte strings and unicode strings are
compatible. No need to feel "uneasy". :)

Stefan
Aug 24 '08 #8
George Sakkis wrote:
It seems xml.etree.cElementTree.iterparse() is not unicode aware:
>>>from StringIO import StringIO
from xml.etree.cElementTree import iterparse
s = u'<name>\u03a0\u03b1\u03bd\u03b1\u03b3\u03b9\u03ce \u03c4\u03b7\u03c2</name>'
for event,elem in iterparse(StringIO(s)):
... print elem.text
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 64, in __iter__
UnicodeEncodeError: 'ascii' codec can't encode characters in position
6-15: ordinal not in range(128)

Am I using it incorrectly or it doesn't currently support unicode ?
If you want to parse XML from Python unicode strings, you can use lxml.etree.
The XML specification allows transport protocols and other sources to provide
external encoding information. lxml supports the Python unicode type as a
transport and reads the internal byte sequence of the unicode string.

To be clear, this does not mean that the parsing happens at the unicode
character level. Parsing XML is about parsing bytes, not characters.

Stefan
Aug 24 '08 #9
On Aug 24, 1:12 am, Stefan Behnel <stefan...@behnel.dewrote:
George Sakkis wrote:
On Aug 21, 1:48 am, Fredrik Lundh <fred...@pythonware.comwrote:
George Sakkis wrote:
It's interesting that the element text attributes after a successful
parse do not necessarily have the same type, i.e. all be str or all
unicode. I ported some text extraction code from BeautifulSoup (which
handles all text as unicode) and I was surprized to find out that in
xml.etree the returned text's type is not fixed, even within the same
file. Although it's not a bug, having a mixed collection of byte and
unicode strings from the same source makes me somewhat uneasy.
If you don't care about memory and execution performance, there are
plenty of toolkits that guarantee that you always get Unicode strings.
As long as they are documented, both approaches are fine for different
cases. Currently the only reference I found about unicode in
ElementTree is "All strings can either be Unicode strings, or 8-bit
strings containing US-ASCII only." [1], which is rather ambiguous

It's not ambiguous in Py2.x, where ASCII byte strings and unicode strings are
compatible. No need to feel "uneasy". :)
It depends on what you mean by "compatible"; e.g. you can't safely do
[s.decode('utf8') for s in strings] if you have byte strings mixed
with unicode.

George
Aug 25 '08 #10
George Sakkis wrote:
It depends on what you mean by "compatible"; e.g. you can't safely do
[s.decode('utf8') for s in strings] if you have byte strings mixed
with unicode.
why would you want to decode strings given to you by a library that
returns decoded strings?

if you meant to write "encode", you can indeed safely do
[s.encode('utf8') for s in strings] as long as all strings are returned
by an ET implementation.

</F>

Aug 25 '08 #11
On Aug 25, 4:45*pm, Fredrik Lundh <fred...@pythonware.comwrote:
George Sakkis wrote:
It depends on what you mean by "compatible"; e.g. you can't safely do
[s.decode('utf8') for s in strings] if you have byte strings mixed
with unicode.

why would you want to decode strings given to you by a library that
returns decoded strings?

if you meant to write "encode", you can indeed safely do
[s.encode('utf8') for s in strings] as long as all strings are returned
by an ET implementation.
I was replying to the general assertion that "in 2.x ASCII byte
strings and unicode strings are compatible", not specifically about
the strings returned by ET.

George
Aug 27 '08 #12
George Sakkis wrote:
>if you meant to write "encode", you can indeed safely do
[s.encode('utf8') for s in strings] as long as all strings are returned
by an ET implementation.

I was replying to the general assertion that "in 2.x ASCII byte
strings and unicode strings are compatible", not specifically about
the strings returned by ET.
that assertion was made in the context of ET. having to unilaterially
change the topic to "win" an argument is pretty lame.

and if you really meant to write "decode", you picked a rather stupid
example to support your complaint about ET not returning Unicode -- your
example does work fine for byte strings (whether they contain pure ASCII
or not), but doesn't work at all for arbitrary Unicode strings, because
decoding things that are already decoded makes very little sense (which
explains why that method was removed in 3.0).
>>"hello".decode("utf-8")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'decode'

are you sure you understand the distinction between Unicode strings and
encoded strings?

</F>

Aug 27 '08 #13
On Aug 27, 5:42 am, Fredrik Lundh <fred...@pythonware.comwrote:
George Sakkis wrote:
if you meant to write "encode", you can indeed safely do
[s.encode('utf8') for s in strings] as long as all strings are returned
by an ET implementation.
I was replying to the general assertion that "in 2.x ASCII byte
strings and unicode strings are compatible", not specifically about
the strings returned by ET.

that assertion was made in the context of ET. having to unilaterially
change the topic to "win" an argument is pretty lame.
I took Stefan's comment as a general statement, not in the context of
ET. Feeling the need to keep "defending" ET at this point is, to
borrow your words, pretty lame.
and if you really meant to write "decode", you picked a rather stupid
example to support your complaint about ET not returning Unicode -- your
example does work fine for byte strings (whether they contain pure ASCII
or not), but doesn't work at all for arbitrary Unicode strings, because
decoding things that are already decoded makes very little sense (which
explains why that method was removed in 3.0).
The thing is, a user might be happily using ET and call "decode" on
the returned byte strings as long as the files happen to be all ASCII,
without knowing that ET may also return Unicode. Then after some weeks/
months/years a non-ASCII file comes in and the program breaks. As far
as I am concerned, it's a documentation issue, nothing more.

George
Aug 27 '08 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Weir | last post by:
I'm sure this is a very simple thing to do, once you know how to do it, but I am having no fun at all trying to write utf-8 strings to a unicode file. Does anyone have a couple of lines of code...
8
by: Bill Eldridge | last post by:
I'm trying to grab a document off the Web and toss it into a MySQL database, but I keep running into the various encoding problems with Unicode (that aren't a problem for me with GB2312, BIG 5,...
8
by: Francis Girard | last post by:
Hi, For the first time in my programmer life, I have to take care of character encoding. I have a question about the BOM marks. If I understand well, into the UTF-8 unicode binary...
48
by: Zenobia | last post by:
Recently I was editing a document in GoLive 6. I like GoLive because it has some nice features such as: * rewrite source code * check syntax * global search & replace (through several files at...
4
by: webdev | last post by:
lo all, some of the questions i'll ask below have most certainly been discussed already, i just hope someone's kind enough to answer them again to help me out.. so i started a python 2.3...
4
by: paul.sherwood | last post by:
Hi Im trying to parse a large(150MB) xml file in order to extract specific required records. import sys from elementtree.ElementTree import ElementTree root = ElementTree(file=big.xml')
2
by: Neil Schemenauer | last post by:
python-dev@python.org.] The PEP has been rewritten based on a suggestion by Guido to change str() rather than adding a new built-in function. Based on my testing, I believe the idea is...
24
by: ChaosKCW | last post by:
Hi I am reading from an oracle database using cx_Oracle. I am writing to a SQLite database using apsw. The oracle database is returning utf-8 characters for euopean item names, ie special...
2
by: Stuart McGraw | last post by:
I have a broad (~200K nodes) but shallow xml file I want to parse with Elementtree. There are too many nodes to read into memory simultaneously so I use iterparse() to process each node...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.