Re: accessing individual characters in unicode strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christian Heimes

    Re: accessing individual characters in unicode strings

    Peter Robinson schrieb:
    Dear list
    I am at my wits end on what seemed a very simple task:
    I have some greek text, nicely encoded in utf8, going in and out of a
    xml database, being passed over and beautifully displayed on the web.
    For example: the most common greek word of all 'kai' (or και if your
    mailer can see utf8)
    So all I want to do is:
    step through this string a character at a time, and do something for
    each character (actually set a width attribute somewhere else for each
    character)
    As John already said: UTF-8 ain't unicode. UTF-8 is an encoding similar
    to ASCII or Latin-1 but different in its inner workings. A single
    character may be encoded by up to 6 bytes.

    I highly recommend Joel's article on unicode:

    The Absolute Minimum Every Software Developer Absolutely, Positively
    Must Know About Unicode and Character Sets (No Excuses!)
    Ever wonder about that mysterious Content-Type tag? You know, the one you’re supposed to put in HTML and you never quite know what it should be? Did you ever get an email from your friends in…


    Christian

  • hdante

    #2
    Re: accessing individual characters in unicode strings

    On Apr 12, 9:48 am, Christian Heimes <li...@cheimes. dewrote:
    Peter Robinson schrieb:
    >
    Dear list
    I am at my wits end on what seemed a very simple task:
    I have some greek text, nicely encoded in utf8, going in and out of a
    xml database, being passed over and beautifully displayed on the web.
    For example: the most common greek word of all 'kai' (or êáé if your
    mailer can see utf8)
    So all I want to do is:
    step through this string a character at a time, and do something for
    each character (actually set a width attribute somewhere else for each
    character)
    >
    As John already said: UTF-8 ain't unicode. UTF-8 is an encoding similar
    to ASCII or Latin-1 but different in its inner workings. A single
    character may be encoded by up to 6 bytes.
    Up to 4 bytes in the latest versions. (the largest value is U+10FFFF
    and is represented by 0xF4 0x8F 0xBF 0xBF).

    I believe the proper way for returning the number of characters for
    Greek would require a normalization first:

    from unicodedata import normalize
    def greek_text_leng th(utf8_string) :
    u = unicode(utf8_st ring, 'utf-8')
    u = normalize('NFC' , u)
    return len(u)

    If there are pairs of characters that count as one, things may be
    worse.
    >
    I highly recommend Joel's article on unicode:
    >
    The Absolute Minimum Every Software Developer Absolutely, Positively
    Must Know About Unicode and Character Sets (No Excuses!)http://www.joelonsoftware.com/articles/Unicode.html
    >
    Christian

    Comment

    Working...