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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:53:35  来源:igfitidea点击:

Convert Unicode/UTF-8 string to lower/upper case using pure & pythonic library

pythongoogle-app-enginecase-conversion

提问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

strencoded in UTF-8 and unicodeare two different types. Don't use string, use the appropriate method on the unicode object:

strUTF-8 编码,unicode是两种不同的类型。不要使用string,在 unicode 对象上使用适当的方法:

>>> print u'?'.upper()
?

Decode strto unicodebefore using:

使用前解码strunicode

>>> print '?'.decode('utf-8').upper()
?