python中土耳其编码范围_Python UTF-8小写土耳其语专用字母

"在Python2.7中,遇到将'I'转换为'ı','İ'转换为'i'等土耳其语字母大小写问题。尝试使用decode("utf-8").lower()未成功。解决方法是通过创建一个映射表进行手动转换。具体做法是创建一个字典lower_map,将'I'和'İ'的Unicode编码映射到对应的土耳其语小写字母,然后使用translate()方法进行转换。"
摘要由CSDN通过智能技术生成

with using python 2.7:

>myCity = 'Isparta'

>myCity.lower()

>'isparta'

#-should be-

>'ısparta'

tried some decoding, (like, myCity.decode("utf-8").lower()) but could not find how to do it.

how can lower this kinds of letters? ('I' > 'ı', 'İ' > 'i' etc)

EDIT: In Turkish, lower case of 'I' is 'ı'. Upper case of 'i' is 'İ'

解决方案

Some have suggested using the tr_TR.utf8 locale. At least on Ubuntu, perhaps related to this bug, setting this locale does not produce the desired result:

import locale

locale.setlocale(locale.LC_ALL, 'tr_TR.utf8')

myCity = u'Isparta İsparta'

print(myCity.lower())

# isparta isparta

So if this bug affects you, as a workaround you could perform this translation yourself:

lower_map = {

ord(u'I'): u'ı',

ord(u'İ'): u'i',

}

myCity = u'Isparta İsparta'

lowerCity = myCity.translate(lower_map)

print(lowerCity)

# ısparta isparta

prints

ısparta isparta

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值