How can I convert Unicode to uppercase to print it?

后端 未结 5 917
遥遥无期
遥遥无期 2020-12-10 01:04

I have this:

>>> print \'example\'
example
>>> print \'exámple\'
exámple
>>> print \'exámple\'.upper()
EXáMPLE

W

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-10 01:23

    In python 2.x, just convert the string to unicode before calling upper(). Using your code, which is in utf-8 format on this webpage:

    >>> s = 'exámple'
    >>> s
    'ex\xc3\xa1mple'  # my terminal is not utf8. c3a1 is the UTF-8 hex for á
    >>> s.decode('utf-8').upper()
    u'EX\xc1MPLE'  # c1 is the utf-16 aka unicode for á
    

    The call to decode takes it from its current format to unicode. You can then convert it to some other format, like utf-8, by using encode. If the character was in, say, iso-8859-2 (Czech, etc, in this case), you would instead use s.decode('iso-8859-2').upper().

    As in my case, if your terminal is not unicode/utf-8 compliant, the best you can hope for is either a hex representation of the characters (like mine) or to convert it lossily using s.decode('utf-8').upper().encode('ascii', 'replace'), which results in 'EX?MPLE'. If you can't make your terminal show unicode, write the output to a file in utf-8 format and open that in your favourite editor.

提交回复
热议问题