![](/img/trans.png)
[英]How to make Python 2.x Unicode strings not print as u'string'?
[英]An equivalent to string.ascii_letters for unicode strings in python 2.x?
在标准库的“字符串”模块中,
string.ascii_letters ## Same as string.ascii_lowercase + string.ascii_uppercase
是
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
是否有一个类似的常量,包括所有被认为是unicode中的字母的东西?
您可以使用以下内容构造自己的Unicode大写和小写字母常量:
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')
这使得字符串长度为2153个字符(缩小的Unicode Python构建)。 对于letter in unicode_letters
代码,使用set代码会更快:
unicode_letters = set(unicode_letters)
没有字符串,但您可以使用unicodedata
模块检查字符是否是字母,特别是其category()
函数。
>>> unicodedata.category(u'a')
'Ll'
>>> unicodedata.category(u'A')
'Lu'
>>> unicodedata.category(u'5')
'Nd'
>>> unicodedata.category(u'ф') # Cyrillic f.
'Ll'
>>> unicodedata.category(u'٢') # Arabic-indic numeral for 2.
'Nd'
Ll
表示“字母,小写”。 Lu
意思是“字母,大写”。 Nd
表示“数字,数字”。
这将是一个相当大的常数。 Unicode目前涵盖超过100,000个不同的字符。 所以答案是否定的。
问题是你为什么需要它? 例如,可能有一些其他方法可以解决unicodedata模块的问题。
更新:您可以从ftp://ftp.unicode.org/下载包含所有unicode数据点名称和其他信息的文件,并使用它做大量有趣的事情。
正如在以前的答案中提到,该字符串确实是太长了。 因此,您必须针对(a)特定语言。
[编辑:我意识到这是我原来的预期用途,对于大多数用途,我猜。 然而,与此同时,Mark Tolonen对这个问题给出了一个很好的答案,所以我选择了他的答案,尽管我使用了以下解决方案]
使用“locale”模块可以轻松完成此操作:
import locale
import string
code = 'fr_FR' ## Do NOT specify encoding (see below)
locale.setlocale(locale.LC_CTYPE, code)
encoding = locale.getlocale()[1]
letters = string.letters.decode(encoding)
“letters”是一个117个字符长的unicode字符串。
显然,string.letters依赖于所选语言代码的默认编码,而不是语言本身。 将语言环境设置为fr_FR或de_DE或es_ES会将string.letters更新为相同的值(因为它们都默认在ISO8859-1中编码)。
如果向语言代码(de_DE.UTF-8)添加编码,则将使用默认编码而不是string.letters。 如果您使用上述其余代码,那将导致UnicodeDecodeError。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.