当我.lower()
在 Python 2.7 中使用时,字符串不会转换为字母的小写ŠČŽ
。我从字典中读取数据。
我尝试使用str(tt["code"]).lower()
, tt["code"].lower()
。
有什么建议么 ?
当我.lower()
在 Python 2.7 中使用时,字符串不会转换为字母的小写ŠČŽ
。我从字典中读取数据。
我尝试使用str(tt["code"]).lower()
, tt["code"].lower()
。
有什么建议么 ?
使用 unicode 字符串:
drostie@signy:~$ 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,你应该使用任何实际编码)。