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

Python string.title() issue with German umlauts

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

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

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

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

有什么想法可以防止元音变音后的大写吗?
我的 Python 版本是 debian linux

上的 2.6.6

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

>>> a = "müller"
>>> a.decode('utf8').title()
u'M\xfcller'
>>> print a.decode('utf8').title()
Müller

您以后可以随时再次编码为 UTF-8。