473,387 Members | 1,578 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,387 software developers and data experts.

XML with Unicode: what am I doing wrong?

This is a followup to a blog post I wrote the other day
http://www.blueskyonmars.com/archive...ementtidy.html

I started out working in the context of elementtidy, but now I am
running into trouble in general Python-XML areas, so I thought I'd toss
the question out here. The code below is fairly self-explanatory. I have
a small HTML snippet that is UTF-8 encoded and is not 7-bit ASCII
compatible. I use Tidy to convert it to XHTML, and this particular setup
returns a unicode instance rather than a string.

import _elementtidy as et
from xml.parsers import expat

data = unicode(open("snippetWithUnicode.html").read(), "utf-8")
html = et.fixup(data)[0]
parser = expat.ParserCreate()
parser.Parse(html)

UnicodeEncodeError: 'ascii' codec can't encode character '\ub5' in
position 542: ordinal not in range(128)

If I set my default encoding to utf8 in sitecustomize.py, it works just
fine. I'm thinking that I can't be the only one trying to pass unicode
to expat... Is there something else I need to do here?

Thanks,
Kevin
Blazing Things
Jul 18 '05 #1
2 2663
> I started out working in the context of elementtidy, but now I am
running into trouble in general Python-XML areas, so I thought I'd toss
the question out here. The code below is fairly self-explanatory. I have
a small HTML snippet that is UTF-8 encoded and is not 7-bit ASCII
compatible. I use Tidy to convert it to XHTML, and this particular setup
returns a unicode instance rather than a string.

import _elementtidy as et
from xml.parsers import expat

data = unicode(open("snippetWithUnicode.html").read(), "utf-8")
html = et.fixup(data)[0]
parser = expat.ParserCreate()
parser.Parse(html)

UnicodeEncodeError: 'ascii' codec can't encode character '\ub5' in
position 542: ordinal not in range(128)

If I set my default encoding to utf8 in sitecustomize.py, it works just
fine. I'm thinking that I can't be the only one trying to pass unicode
to expat... Is there something else I need to do here?


you confuse unicode with utf8. Expat can parse the latter - the former is
internal to python. And passing it to something that needs a string will
result in a conversion - which fails because of the ascii encoding.

Do this:

parser.Parse(html.encode('utf-8'))

--
Regards,

Diez B. Roggisch
Jul 18 '05 #2
In article <ct*************@news.t-online.com>,
"Diez B. Roggisch" <de*********@web.de> wrote:
I started out working in the context of elementtidy, but now I am
running into trouble in general Python-XML areas, so I thought I'd toss
the question out here. The code below is fairly self-explanatory. I have
a small HTML snippet that is UTF-8 encoded and is not 7-bit ASCII
compatible. I use Tidy to convert it to XHTML, and this particular setup
returns a unicode instance rather than a string.

import _elementtidy as et
from xml.parsers import expat

data = unicode(open("snippetWithUnicode.html").read(), "utf-8")
html = et.fixup(data)[0]
parser = expat.ParserCreate()
parser.Parse(html)

UnicodeEncodeError: 'ascii' codec can't encode character '\ub5' in
position 542: ordinal not in range(128)

If I set my default encoding to utf8 in sitecustomize.py, it works just
fine. I'm thinking that I can't be the only one trying to pass unicode
to expat... Is there something else I need to do here?


you confuse unicode with utf8. Expat can parse the latter - the former is
internal to python. And passing it to something that needs a string will
result in a conversion - which fails because of the ascii encoding.

Do this:

parser.Parse(html.encode('utf-8'))


Possibly preceded by

parser = expat.ParserCreate('utf-8')

...so there's no confusion with the declared encoding, in case that's not
utf-8.

Just
Jul 18 '05 #3

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

Similar topics

19
by: Gerson Kurz | last post by:
AAAAAAAARG I hate the way python handles unicode. Here is a nice problem for y'all to enjoy: say you have a variable thats unicode directory = u"c:\temp" Its unicode not because you want it...
14
by: wolfgang haefelinger | last post by:
Hi, I wonder whether someone could explain me a bit what's going on here: import sys # I'm running Mandrake 1o and Windows XP. print sys.version ## 2.3.3 (#2, Feb 17 2004, 11:45:40)
12
by: Chris Mullins | last post by:
I'm implementing RFC 3491 in .NET, and running into a strange issue. Step 1 of RFC 3491 is performing a set of mappings dicated by tables B.1 and B.2. I'm having trouble with the following...
1
by: Dawid Kuroczko | last post by:
I'm trying to use tsearch2 with database which is in 'UNICODE' encoding. It works fine for English text, but as I intend to search Polish texts I did: insert into pg_ts_cfg('default_polish',...
2
by: Alex Guryanow | last post by:
Hi, I have windows app written in Borland C++ Builder 5.0. Using ODBC driver windows app connects to database on linux server. Database is created with UNICODE encoding. When pg-server is...
12
by: damjan | last post by:
This may look like a silly question to someone, but the more I try to understand Unicode the more lost I feel. To say that I am not a beginner C++ programmer, only had no need to delve into...
22
by: Filipe | last post by:
Hi all, I'm starting to learn python but am having some difficulties with how it handles the encoding of data I'm reading from a database. I'm using pymssql to access data stored in a SqlServer...
14
by: abhi147 | last post by:
Hi , I want to convert an array of bytes like : {79,104,-37,-66,24,123,30,-26,-99,-8,80,-38,19,14,-127,-3} into Unicode character with ISO-8859-1 standard. Can anyone help me .. how should...
6
by: Bill Nguyen | last post by:
Below are sometext I extracted from a mySQL database. How can I decode them so that I can read them in Unicode? Thanks Bill ------------ Virginia Hamilton Adair / Lâm Thị Mỹ...
8
by: miztaken | last post by:
Hi, I have a string with some unicode characters and when i tried to displayed it in TreeView its not displaying. I am doing like this: TreeNode tn = new TreeNode("unicodestring");...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.