471,348 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 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 3944
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Stewart Midwinter | last post: by
1 post views Thread by Greg Wilson | last post: by
1 post views Thread by mirandacascade | last post: by
15 posts views Thread by Steven Bethard | last post: by
reply views Thread by Greg Aumann | last post: by
2 posts views Thread by mirandacascade | last post: by
5 posts views Thread by saif.shakeel | last post: by
1 post views Thread by Mike Slinn | last post: by
3 posts views Thread by gray.bowman | last post: by
1 post views Thread by Ronak mishra | last post: by

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.