當我在Python 2.7中使用.lower()
時,字符串不會被轉換成小寫字母ŠČŽ
。 我從字典中讀取數據。python 2.7小寫
我試過使用str(tt["code"]).lower()
,tt["code"].lower()
。
有什麼建議嗎?
當我在Python 2.7中使用.lower()
時,字符串不會被轉換成小寫字母ŠČŽ
。 我從字典中讀取數據。python 2.7小寫
我試過使用str(tt["code"]).lower()
,tt["code"].lower()
。
有什麼建議嗎?
使用Unicode字符串:
[email protected]:~$ python
Python 2.7.2+ (default, Oct 4 2011, 20:06:09)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "ŠČŽ"
ŠČŽ
>>> print "ŠČŽ".lower()
ŠČŽ
>>> print u"ŠČŽ".lower()
ščž
見那個小u
?這意味着它創建爲unicode
對象而不是str
對象。
使用Unicode:
>>> print u'ŠČŽ'.lower().encode('utf8')
ščž
>>>
您需要將您的文本儘快UNICODE ,因爲它從外界進入你的程序,而不是僅僅在轉換點上,你會注意到一個問題。
因此,要麼使用codecs
模塊讀取解碼文本,要麼使用'bytestring'.decode('latin2')
(其中latin2的位置應該使用任何實際的編碼)。
看看http://stackoverflow.com/questions/727507/how-can-i-do-unicode-uppercase,我想它可能是相關的。 – mgilson 2012-03-30 12:45:44