22

当我.lower()在 Python 2.7 中使用时,字符串不会转换为字母的小写ŠČŽ。我从字典中读取数据。

我尝试使用str(tt["code"]).lower(), tt["code"].lower()

有什么建议么 ?

4

2 回答 2

29

使用 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对象创建的。

于 2012-03-30T12:45:36.317 回答
4

使用 unicode:

>>> print u'ŠČŽ'.lower().encode('utf8')
ščž
>>>

您需要在文本从外部世界进入您的程序后立即将其转换为 unicode ,而不仅仅是在您发现问题时。

因此,要么使用codecs模块读入解码文本,要么使用'bytestring'.decode('latin2')(代替 latin2,你应该使用任何实际编码)。

于 2012-03-30T12:45:08.750 回答