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
尝试通过将语言环境设置为德语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.
| 归档时间: | 
 | 
| 查看次数: | 1659 次 | 
| 最近记录: |