473,809 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

elementtree w/utf8

Hi, I'm getting the by-now-familiar error:
return codecs.charmap_ decode(input,er rors,decoding_m ap)
UnicodeEncodeEr ror: '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 TidyHTMLTreeBui lder, but no luck. Here's my code:
from elementtree import ElementTree as ET
from elementtidy import TidyHTMLTreeBui lder

fd = codecs.open(htm file,encoding=' utf-8')
tidyTree =
TidyHTMLTreeBui lder.TidyHTMLTr eeBuilder(encod ing='utf-8')
tidyTree.feed(f d.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 4036
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,er rors,decoding_m ap)
UnicodeEncodeEr ror: '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 TidyHTMLTreeBui lder, but no luck. Here's my code:
from elementtree import ElementTree as ET
from elementtidy import TidyHTMLTreeBui lder

fd = codecs.open(htm file,encoding=' utf-8')
tidyTree =
TidyHTMLTreeBui lder.TidyHTMLTr eeBuilder(encod ing='utf-8')
tidyTree.feed(f d.read())
self.tree = tidyTree.close( )
fd.close()

what am I doing wrong? Thanks in advance.
You feed decoded data to `TidyHTMLTreeBu ilder`. 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,er rors,decoding_m ap)
UnicodeEncodeEr ror: '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 TidyHTMLTreeBui lder, but no luck. Here's my code:
from elementtree import ElementTree as ET
from elementtidy import TidyHTMLTreeBui lder

fd = codecs.open(htm file,encoding=' utf-8')
tidyTree =
TidyHTMLTreeBui lder.TidyHTMLTr eeBuilder(encod ing='utf-8')
tidyTree.feed(f d.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
TidyHTMLTreeBui lder 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.net wrote 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,er rors,decoding_m ap)
UnicodeEncodeE rror: '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 TidyHTMLTreeBui lder, but no luck. Here's my code:
from elementtree import ElementTree as ET
from elementtidy import TidyHTMLTreeBui lder

fd = codecs.open(htm file,encoding=' utf-8')
tidyTree =
TidyHTMLTreeBu ilder.TidyHTMLT reeBuilder(enco ding='utf-8')
tidyTree.feed(f d.read())
self.tree = tidyTree.close( )
fd.close()

what am I doing wrong? Thanks in advance.

You feed decoded data to `TidyHTMLTreeBu ilder`. 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.dewr ote in message
news:47******** ******@web.de.. .
Tim Arnold wrote:
>On a related note, I have another question--where/how can I get the
cElementTree.p y 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
3282
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" age="49"></person> <person><name="hilda" sex="female" age="33"></person> <person><name="bartholomew" sex="male" age="17"> </person> </population>
1
3092
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: ---------------------------------------------------------------------- #!/usr/bin/env python import sys, os from elementtree import ElementTree
1
3073
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
5433
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 encodings, so I'm sure I'm just screwing something simple up. Can anyone help me? Here's the interactive session. Sorry it's a little verbose, but I figured it would be better to include too much than not enough. I basically expected...
0
1777
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 be able to write it out either as XML or in the original format. I really want the api for the XML-like language to be the same as the elementtree api to reduce confusion, ease of learning etc. In reading the elementtree documentation I found...
2
1969
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 the XML document 2) The inherited code is somewhat 'brittle' in that some well-formed XML documents are not correctly processed by the code; the brittleness is caused by how the parser portion of the code handles whitespace. 3) I would like to...
5
9993
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": t.set("Semantics", "dataPackageID")
1
294
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 = tree.getroot() ns = 'http://earth.google.com/kml/2.1' for folder in kml.findall("{%s}Folder/{%s}Folder/{%s}name" % (ns, ns, ns)): print folder.text
3
2090
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 available, gives me ... ElementTree(sectionElement).write("section.xml") TypeError: 'module' object is not callable I'm new to python, so I think I'm doing something wrong.. any
0
9603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4333
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.