[I18n-sig] Unicode Tutorial (Slow) Progress

M.-A. Lemburg mal@lemburg.com
Wed, 05 Apr 2000 20:41:30 +0200


Andy Robinson wrote:
> 
> I'm part way through a tutorial at long last.  My own work is pretty poor so
> far, but it DOES include Marc-Andre's 'console session' demos at the bottom
> which show the current usage.
> 
> http://www.reportlab.com/i18n/python_unicode_tutorial.html
> 
> If anyone can suggest topics I should cover (apart from the obvious one of
> using every new features at least once) or simple relevant examples, I'll
> try to work them in over the coming weeks.

Looks great... a bit much exposure, maybe ;-)

Note that the stackable stream example will need a small bit
of updating (the return is wrong -- the API was changed since I
programmed the example):

import codecs,sys

# Convert Unicode -> UTF-8
(e,d,sr,sw) = codecs.lookup('utf-8')
unicode_to_utf8 = sw(sys.stdout)

# Convert Latin-1 -> Unicode during .write
(e,d,sr,sw) = codecs.lookup('latin-1')
class StreamRewriter(codecs.StreamWriter):

    encode = e
    decode = d

    def write(self,object):

        """ Writes the object's contents encoded to self.stream
            and returns the number of bytes written.
        """
        data,consumed = self.decode(object,self.errors)
        self.stream.write(data)
    
latin1_to_utf8 = StreamRewriter(unicode_to_utf8)

# Now install
sys.stdout = latin1_to_utf8

# All subsequent prints will output Latin-1 strings using UTF-8
# characters...
print 'Hello World !'
print 'Héllò Wörld !'
print 'ÄÖÜäöüß'


-- 
Marc-Andre Lemburg
______________________________________________________________________
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/