An equivalent to string.ascii_letters for unicode strings in python 2.x?

后端 未结 4 1860
天命终不由人
天命终不由人 2020-12-16 00:30

In the \"string\" module of the standard library,

string.ascii_letters ## Same as string.ascii_lowercase + string.ascii_uppercase

is

4条回答
  •  自闭症患者
    2020-12-16 00:36

    You can construct your own constant of Unicode upper and lower case letters with:

    import unicodedata as ud
    all_unicode = ''.join(unichr(i) for i in xrange(65536))
    unicode_letters = ''.join(c for c in all_unicode
                              if ud.category(c)=='Lu' or ud.category(c)=='Ll')
    

    This makes a string 2153 characters long (narrow Unicode Python build). For code like letter in unicode_letters it would be faster to use a set instead:

    unicode_letters = set(unicode_letters)
    

提交回复
热议问题