python 使用纯 pythonic 库将 Unicode/UTF-8 字符串转换为小写/大写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2145826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert Unicode/UTF-8 string to lower/upper case using pure & pythonic library
提问by Viet
I use Google App Engine and cannot use any C/C++ extension, just pure & pythonic library to do conversion of Unicode/UTF-8 strings to lower/upper case. str.lower() and string.lowercase() don't.
我使用 Google App Engine,不能使用任何 C/C++ 扩展,只能使用纯 pythonic 库来将 Unicode/UTF-8 字符串转换为小写/大写。str.lower() 和 string.lowercase() 没有。
回答by Ignacio Vazquez-Abrams
str
encoded in UTF-8 and unicode
are two different types. Don't use string
, use the appropriate method on the unicode object:
str
UTF-8 编码,unicode
是两种不同的类型。不要使用string
,在 unicode 对象上使用适当的方法:
>>> print u'?'.upper()
?
Decode str
to unicode
before using:
使用前解码str
为unicode
:
>>> print '?'.decode('utf-8').upper()
?