Python string.title()问题与德语变音符号

caz*_*ler 3 python string utf-8 diacritics

如果字符串包含德语变音符号(üöä),我会遇到Python string.title()函数的奇怪行为.然后,不仅字符串的第一个字符大写,而且变音字符后面的字符也是大写的.

# -*- coding: utf-8 -*-
a = "müller"
print a.title()
# this returns >MüLler< , not >Müller< as expected
Run Code Online (Sandbox Code Playgroud)

尝试通过将语言环境设置为德语UTF-8字符集来修复,但没有成功:

import locale
locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')
a="müller"
print a.title()
# same value >MüLler<
Run Code Online (Sandbox Code Playgroud)

在破折号之后有任何阻止大写的想法吗?
我的Python版本在debian linux上是2.6.6

Mar*_*ers 5

将您的字符串解码为Unicode,然后使用unicode.title():

>>> a = "müller"
>>> a.decode('utf8').title()
u'M\xfcller'
>>> print a.decode('utf8').title()
Müller
Run Code Online (Sandbox Code Playgroud)

您以后可以再次编码为UTF-8.