UTF8 & HTMLParser

J

Jan Danielsson

Hello all,

I'm writing a python script which fetches a HTML-page (using wget),
and then parses the retrieved page using a custom htmllib HTMLParser.

The page I fetch is encoded in utf8, and my text-handler currently
looks like this:

def handle_data(self, text):
if self.inOption:
self.currentName = text

However, I would like to convert the "text" (which is utf8) to
latin-1. How do I do that? I've been trying to figure it out for some
time now, and I'm just getting frustrated. :-(
 
J

Jan Danielsson

Jan said:
Hello all,

I'm writing a python script which fetches a HTML-page (using wget),
and then parses the retrieved page using a custom htmllib HTMLParser.

The page I fetch is encoded in utf8, and my text-handler currently
looks like this:

def handle_data(self, text):
if self.inOption:
self.currentName = text

However, I would like to convert the "text" (which is utf8) to
latin-1. How do I do that? I've been trying to figure it out for some
time now, and I'm just getting frustrated. :-(

I should have mentioned: The problem appears to be that I can't seem
to find a way to make python understand that "text" (the above argument)
is in fact already utf-8.
 
K

Klaus Alexander Seistrup

Jan said:
However, I would like to convert the "text" (which is utf8)
to latin-1. How do I do that?

How about:

latin = unicode(text, 'utf-8').encode('iso-8859-1')

Please see help(u''.encode) for details about error handling. You
might also want to trap errors in a try-except statement.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top