472,982 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

elementtree w/utf8

Hi, I'm getting the by-now-familiar error:
return codecs.charmap_decode(input,errors,decoding_map)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa9' in position
4615: ordinal not in range(128)

the html file I'm working with is in utf-8, I open it with codecs, try to
feed it to TidyHTMLTreeBuilder, but no luck. Here's my code:
from elementtree import ElementTree as ET
from elementtidy import TidyHTMLTreeBuilder

fd = codecs.open(htmfile,encoding='utf-8')
tidyTree =
TidyHTMLTreeBuilder.TidyHTMLTreeBuilder(encoding=' utf-8')
tidyTree.feed(fd.read())
self.tree = tidyTree.close()
fd.close()

what am I doing wrong? Thanks in advance.

On a related note, I have another question--where/how can I get the
cElementTree.py module? Sorry for something so basic, but I tried installing
cElementTree, but while I could compile with setup.py build, I didn't end up
with a cElementTree.py file anywhere. The directory structure on my system
(HPux, but no root access) doesn't work well with setup.py install.

thanks,
--Tim Arnold
Oct 25 '07 #1
6 4006
On Thu, 25 Oct 2007 17:15:36 -0400, Tim Arnold wrote:
Hi, I'm getting the by-now-familiar error:
return codecs.charmap_decode(input,errors,decoding_map)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa9' in position
4615: ordinal not in range(128)

the html file I'm working with is in utf-8, I open it with codecs, try to
feed it to TidyHTMLTreeBuilder, but no luck. Here's my code:
from elementtree import ElementTree as ET
from elementtidy import TidyHTMLTreeBuilder

fd = codecs.open(htmfile,encoding='utf-8')
tidyTree =
TidyHTMLTreeBuilder.TidyHTMLTreeBuilder(encoding=' utf-8')
tidyTree.feed(fd.read())
self.tree = tidyTree.close()
fd.close()

what am I doing wrong? Thanks in advance.
You feed decoded data to `TidyHTMLTreeBuilder`. As the `encoding`
argument suggests this class wants bytes not unicode. Decoding twice
doesn't work.

Ciao,
Marc 'BlackJack' Rintsch
Oct 25 '07 #2
Tim Arnold schrieb:
Hi, I'm getting the by-now-familiar error:
return codecs.charmap_decode(input,errors,decoding_map)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa9' in position
4615: ordinal not in range(128)

the html file I'm working with is in utf-8, I open it with codecs, try to
feed it to TidyHTMLTreeBuilder, but no luck. Here's my code:
from elementtree import ElementTree as ET
from elementtidy import TidyHTMLTreeBuilder

fd = codecs.open(htmfile,encoding='utf-8')
tidyTree =
TidyHTMLTreeBuilder.TidyHTMLTreeBuilder(encoding=' utf-8')
tidyTree.feed(fd.read())
self.tree = tidyTree.close()
fd.close()

what am I doing wrong? Thanks in advance.
Being to clever for your own good.. sorry to say so. But
TidyHTMLTreeBuilder takes the encoding for a reason: it expects a
byte-string that it will decode itself.

But you decode first, creating a unicode-object. When feeding that to
the string-expecting feed-method, python attempts a conversion to a
byte-string using the default-encoding.

Not using codecs but a file instead should do the trick.

diez
Oct 25 '07 #3

"Marc 'BlackJack' Rintsch" <bj****@gmx.netwrote in message
news:5o************@mid.uni-berlin.de...
On Thu, 25 Oct 2007 17:15:36 -0400, Tim Arnold wrote:
>Hi, I'm getting the by-now-familiar error:
return codecs.charmap_decode(input,errors,decoding_map)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa9' in
position
4615: ordinal not in range(128)

the html file I'm working with is in utf-8, I open it with codecs, try to
feed it to TidyHTMLTreeBuilder, but no luck. Here's my code:
from elementtree import ElementTree as ET
from elementtidy import TidyHTMLTreeBuilder

fd = codecs.open(htmfile,encoding='utf-8')
tidyTree =
TidyHTMLTreeBuilder.TidyHTMLTreeBuilder(encoding= 'utf-8')
tidyTree.feed(fd.read())
self.tree = tidyTree.close()
fd.close()

what am I doing wrong? Thanks in advance.

You feed decoded data to `TidyHTMLTreeBuilder`. As the `encoding`
argument suggests this class wants bytes not unicode. Decoding twice
doesn't work.

Ciao,
Marc 'BlackJack' Rintsch
well now that you say it, it seems so obvious...
some day I will get the hang of this encode/decode stuff. When I read about
it, I'm fine, it makes sense, etc. maybe even a little boring. And then I
write stuff like the above!

Thanks to you and Diez for straightening me out.
--Tim
Oct 26 '07 #4

Tim Arnold wrote:
<snip>
On a related note, I have another question--where/how can I get the
cElementTree.py module? Sorry for something so basic, but I tried installing
cElementTree, but while I could compile with setup.py build, I didn't end up
with a cElementTree.py file anywhere. The directory structure on my system
(HPux, but no root access) doesn't work well with setup.py install.

thanks,
--Tim Arnold
I had the same question a while ago .... and the answer is ElementTree
is now
part of the standard library.

http://docs.python.org/lib/module-xm...ementTree.html

Ross

Oct 28 '07 #5
Tim Arnold wrote:
On a related note, I have another question--where/how can I get the
cElementTree.py module? Sorry for something so basic, but I tried installing
cElementTree, but while I could compile with setup.py build, I didn't end up
with a cElementTree.py file anywhere.
That's because it compiles into a binary extension module, not a plain Python
module (mind the 'c' in its name, which stands for the C language here).

I don't know what the standard library extension is under HP-UX, but look a
little closer at the files that weren't there before, you'll find it.
Depending on what you did to build it, it might also end up in the "build"
directory or as an installable package in the "dist" directory.

The directory structure on my system
(HPux, but no root access) doesn't work well with setup.py install.
That shouldn't be a problem as long as you keep the binary in your PYTHONPATH.

As suggested before, if you have Python 2.5, you don't even need to install it
yourself.

Stefan
Oct 28 '07 #6

"Stefan Behnel" <st******************@web.dewrote in message
news:47**************@web.de...
Tim Arnold wrote:
>On a related note, I have another question--where/how can I get the
cElementTree.py module? Sorry for something so basic, but I tried
installing
cElementTree, but while I could compile with setup.py build, I didn't end
up
with a cElementTree.py file anywhere.

That's because it compiles into a binary extension module, not a plain
Python
module (mind the 'c' in its name, which stands for the C language here).

I don't know what the standard library extension is under HP-UX, but look
a
little closer at the files that weren't there before, you'll find it.
Depending on what you did to build it, it might also end up in the "build"
directory or as an installable package in the "dist" directory.

>The directory structure on my system
(HPux, but no root access) doesn't work well with setup.py install.

That shouldn't be a problem as long as you keep the binary in your
PYTHONPATH.

As suggested before, if you have Python 2.5, you don't even need to
install it
yourself.

Stefan
very nice--thanks. I saw the cElementTree.sl file, but didn't realize it
would work as-is.
thanks,
--Tim
Oct 29 '07 #7

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

Similar topics

7
by: Stewart Midwinter | last post by:
I want to parse a file with ElementTree. My file has the following format: <!-- file population.xml --> <?xml version='1.0' encoding='utf-8'?> <population> <person><name="joe" sex="male"...
1
by: Greg Wilson | last post by:
I'm trying to convert from minidom to ElementTree for handling XML, and am having trouble with entities in DTDs. My Python script looks like this: ...
1
by: mirandacascade | last post by:
O/S: Windows 2K Vsn of Python: 2.4 Currently: 1) Folder structure: \workarea\ <- ElementTree files reside here \xml\ \dom\
15
by: Steven Bethard | last post by:
I'm having trouble using elementtree with an XML file that has some gbk-encoded text. (I can't read Chinese, so I'm taking their word for it that it's gbk-encoded.) I always have trouble with...
0
by: Greg Aumann | last post by:
I am trying to write some python code for a library that reads an XML-like language from a file into elementtree data structures. Then I want to be able to read and/or modify the structure and then...
2
by: mirandacascade | last post by:
Situation is this: 1) I have inherited some python code that accepts a string object, the contents of which is an XML document, and produces a data structure that represents some of the content of...
5
by: saif.shakeel | last post by:
#!/usr/bin/env python from elementtree import ElementTree as Element tree = et.parse("testxml.xml") for t in tree.getiterator("SERVICEPARAMETER"): if t.get("Semantics") == "localId":...
1
by: Mike Slinn | last post by:
The following short Python program parses a KML file and displays the names of all Marks and Routes: from elementtree.ElementTree import ElementTree tree = ElementTree(file='test.kml') kml =...
3
by: gray.bowman | last post by:
I'm messing around with trying to write an xml file using xml.etree.ElementTree. All the examples on the internet show the use of ElementTree.write(), although when I try to use it it's not...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.