python 2.7小写
在Python2.7中使用python 2.7小写,python,python-2.7,unicode,lowercase,python-unicode,Python,Python 2.7,Unicode,Lowercase,Python Unicode,在Python2.7中使用.lower()时,字母的字符串不会转换为小写。 我从字典里读数据 我尝试使用str(tt[“code”]).lower(),tt[“code”].lower() 有什么建议吗?使用unicode: >>> print u'ŠČŽ'.lower().encode('utf8') ščž >>> 您需要在文本从外部世界进入您的程序时立即将其转换为unicode,而不仅仅是在您注意到问题时 因此,可以使用编解码器模块读取解码文本,也可以
.lower()
时,字母的字符串不会转换为小写。
我从字典里读数据
我尝试使用str(tt[“code”]).lower()
,tt[“code”].lower()
有什么建议吗?使用unicode:
>>> print u'ŠČŽ'.lower().encode('utf8')
ščž
>>>
您需要在文本从外部世界进入您的程序时立即将其转换为unicode,而不仅仅是在您注意到问题时
因此,可以使用编解码器
模块读取解码文本,也可以使用'bytestring'。decode('latin2')
(在这里,您应该使用任何实际编码来代替latin2)。使用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
对象。看一看,我认为它可能是相关的。我在读dict,所以如何将tt[“code”]转换为u“ŠŽ”?使用unicode(tt[“code”],“latin2”),其中使用了“latin2”编码,因此,您可能需要使用不同的。还要注意,unicode.lower()
依赖于语言环境。它可能会给出不同的结果,具体取决于它运行的环境。@SvenMarnach:的确,它是与语言环境相关的,但由于语言环境的不同而产生的差异是最小的,接近于由于不使用Unicode而产生的差异——因为在这种情况下,上下两种语言只能理解asciianyway@Yebach字体读这篇文章,这将对您有很大帮助:-然后-使用“decode”string方法将您的字符串转换为unicodeI am reading from dict,那么如何将tt[“code”]转换为u“ŽŽ”?我不能使用ustr(tt[“code”]).lower().encode('utf8')或str(tt[u“code”]).lower().encode('utf8'))