Python和土耳其语大写

min*_*als 5 python turkish locale

我没有找到关于如何在Windows上处理此问题的良好描述,所以我在这里做.

Turkish ?(I)和i(?)中有两个字母由python错误处理.

>>> [char for char in 'May?s']
['M', 'a', 'y', 'i', 's']

>>> '?'.upper().lower()
'i'
Run Code Online (Sandbox Code Playgroud)

应该如何,鉴于区域设置是正确的:

>>> [char for char in 'May?s']
['M', 'a', 'y', '?', 's']

>>> '?'.upper().lower()
'?'
Run Code Online (Sandbox Code Playgroud)

和

>>> 'i'.upper()
'?'

>>> '?'.upper()
'I'
Run Code Online (Sandbox Code Playgroud)

我试过locale.setlocale(locale.LC_ALL,'Turkish_Turkey.1254')甚至'?'.encode('cp857')但它没有帮助.

如何让python正确处理这两个字母?

ism*_*ail 7

你应该使用PyICU

>>> from icu import UnicodeString, Locale
>>> tr = Locale("TR")
>>> s = UnicodeString("i")
>>> print(unicode(s.toUpper(tr)))
?
>>> s = UnicodeString("I")
>>> print(unicode(s.toLower(tr)))
?
>>>
Run Code Online (Sandbox Code Playgroud)


小智 5

您可以为土耳其语字符问题定义自己的硬编码函数。

\n\n
import re\n\ndef tr_upper(self):\n    self = re.sub(r"i", "\xc4\xb0", self)\n    self = re.sub(r"\xc4\xb1", "I", self)\n    self = re.sub(r"\xc3\xa7", "\xc3\x87", self)\n    self = re.sub(r"\xc5\x9f", "\xc5\x9e", self)\n    self = re.sub(r"\xc3\xbc", "\xc3\x9c", self)\n    self = re.sub(r"\xc4\x9f", "\xc4\x9e", self)\n    self = self.upper() # for the rest use default upper\n    return self\n\n\ndef tr_lower(self):\n    self = re.sub(r"\xc4\xb0", "i", self)\n    self = re.sub(r"I", "\xc4\xb1", self)\n    self = re.sub(r"\xc3\x87", "\xc3\xa7", self)\n    self = re.sub(r"\xc5\x9e", "\xc5\x9f", self)\n    self = re.sub(r"\xc3\x9c", "\xc3\xbc", self)\n    self = re.sub(r"\xc4\x9e", "\xc4\x9f", self)\n    self = self.lower() # for the rest use default lower\n    return self\n
Run Code Online (Sandbox Code Playgroud)\n\n

常规鞋面:

\n\n
>>>print("ulvido".upper())\nULVIDO\n
Run Code Online (Sandbox Code Playgroud)\n\n

我们的定制鞋面:

\n\n
>>>print(tr_upper("ulvido"))\nULV\xc4\xb0DO\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果您经常需要这种转换,您可以将其制作为 .py 文件。例如:将其另存为 trtextstyle.py 并导入到您的项目中。

\n\n

如果 trtextstyle.py 与您的文件位于同一目录:

\n\n
from .trtextstyle import tr_upper, tr_lower\n
Run Code Online (Sandbox Code Playgroud)\n\n

希望这可以帮助。

\n