更改包含重音和本地字母的unicode字符串中的字母大小写

Fal*_*gel 5 python string unicode case-sensitive

Python字符串和unicode对象具有以下用于字符串大小写转换的方法.

  • upper()
  • lower()
  • title()

使用unicode字符串,我可以处理本地字母表中的几乎所有字符:

test_str = u"ças ?ak ürt örkl"
print test_str.upper()
>> ÇAS ?AK ÜRT ÖRKL
Run Code Online (Sandbox Code Playgroud)

除了两个字母.由于我住在土耳其,我有典型的Turkish I problem.

在我当地的字母表中,我们有一个?类似的字母,I他们的案例转换必须如下

I ? lowercase ? ?

i ? uppercase ? ?

是的,它败坏的ASCII转换i --> I,因为iI是两个不同的字母.

test_str = u"ik"
print test_str.upper()
>> IK  # Wrong! must be ?K
test_str = u"IK"
print test_str.lower()
>> ik  # Wrong! must be ?k
Run Code Online (Sandbox Code Playgroud)

我怎么能克服这个?有没有办法使用python内置函数正确处理大小写转换?

bob*_*nce 5

Python 目前不支持特定于区域设置的大小写折叠或 Unicode SpecialCasing.txt 中的其他规则。如果您今天需要,可以从PyICU获取它们。

\n\n
>>> unicode( icu.UnicodeString(u\'IK\').toLower(icu.Locale(\'TR\')) )\nu\'\xc4\xb1k\'\n
Run Code Online (Sandbox Code Playgroud)\n\n

尽管如果您只关心土耳其语 I,您可能更愿意将其特殊化。

\n