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正确处理这两个字母?
你应该使用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\nimport 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\nRun Code Online (Sandbox Code Playgroud)\n\n常规鞋面:
\n\n>>>print("ulvido".upper())\nULVIDO\nRun Code Online (Sandbox Code Playgroud)\n\n我们的定制鞋面:
\n\n>>>print(tr_upper("ulvido"))\nULV\xc4\xb0DO\nRun Code Online (Sandbox Code Playgroud)\n\n如果您经常需要这种转换,您可以将其制作为 .py 文件。例如:将其另存为 trtextstyle.py 并导入到您的项目中。
\n\n如果 trtextstyle.py 与您的文件位于同一目录:
\n\nfrom .trtextstyle import tr_upper, tr_lower\nRun Code Online (Sandbox Code Playgroud)\n\n希望这可以帮助。
\n| 归档时间: |
|
| 查看次数: |
1276 次 |
| 最近记录: |