如何使用结构名称用另一种语言?

2024-06-25 06:42:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用此代码块:

>>> import re
>>> def titlecase(s):
...     return re.sub(r"[A-Za-z]+('[A-Za-z]+)?",
...                   lambda mo: mo.group(0)[0].upper() +
...                              mo.group(0)[1:].lower(),
...                   s)
...
>>> titlecase("they're bill's friends.")
"They're Bill's Friends."

它来自Python的文档。在

如果字符串包含一个土耳其语字符,比如'o',string将变成

“雷布”。为了支持所有语言,我应该写些什么?在


Tags: lambda代码importrereturndefgroupupper
2条回答

使用Unicode字符属性数据库,通过使用flags=re.UNICODE编译正则表达式:

def titlecase(s):
    return re.sub(re.compile(r"[\w]+('[\w]+)?", flags=re.UNICODE),
                  lambda mo: mo.group(0)[0].upper() +
                             mo.group(0)[1:].lower(),
                  s)

在python2上,需要使用Unicode字符串:

^{pr2}$

使用unicode字符串,即titlecase(u'börek')。在

相关问题 更多 >