How do I case fold a string in Python 2?

后端 未结 3 1011
情话喂你
情话喂你 2020-12-16 01:00

Python 3.3 adds the casefold method to the str type, but in 2.x I don\'t have anything. What\'s the best way to work around this?

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-16 01:28

    If PyICU is already installed; you could use it to define casefold(). Using the same example strings as in @Russ' answer:

    >>> import icu
    >>> casefold = lambda u: unicode(icu.UnicodeString(u).foldCase())
    >>> print casefold(u"tschüß")
    tschüss
    >>> casefold(u"ΣίσυφοςfiÆ") == casefold(u"ΣΊΣΥΦΟσFIæ") == u"σίσυφοσfiæ"
    True
    >>> icu.UNICODE_VERSION
    '6.3'
    >>> import unicodedata
    >>> unicodedata.unidata_version
    '5.2.0'
    

    The result may depend on the version of Unicode standard.

提交回复
热议问题