[Python-Dev] Unicode proposal: %-formatting ?

M.-A. Lemburg mal@lemburg.com
Thu, 11 Nov 1999 16:59:21 +0100


I wonder how we could add %-formatting to Unicode strings without
duplicating the PyString_Format() logic.

First, do we need Unicode object %-formatting at all ?

Second, here is an emulation using strings and <default encoding>
that should give an idea of one could work with the different
encodings:

    s = '%s %i abcäöü' # a Latin-1 encoded string
    t = (u,3)

    # Convert Latin-1 s to a <default encoding> string via Unicode
    s1 = unicode(s,'latin-1').encode()

    # The '%s' will now add u in <default encoding>
    s2 = s1 % t

    # Finally, convert the <default encoding> encoded string to Unicode
    u1 = unicode(s2)

Note that .encode() defaults to the current setting of
<default encoding>.

Provided u maps to Latin-1, an alternative would be:

    u1 = unicode('%s %i abcäöü' % (u.encode('latin-1'),3), 'latin-1')

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