Python3 .title()的utf-8字符串

lqd*_*qdc 5 python python-3.x

所以我有一个字符串:

天使爱美丽

以字节为单位 b'ame\xcc\x81lie'

在utf-8中,角色结合了前一个角色的重音符号http://www.fileformat.info/info/unicode/char/0301/index.htm

u'ame\u0301lie'

当我这样做:'amélie'.title()在那个字符串上,我得到'AméLie',这对我来说毫无意义.

我知道我可以做一个解决方法,但这是预期的行为还是一个错误?我希望"l"不会被大写.

另一个实验:

  In [1]: [ord(c) for c in 'ame?lie'.title()]
  Out[1]: [65, 109, 101, 769, 76, 105, 101]

  In [2]: [ord(c) for c in 'ame?lie']
  Out[2]: [97, 109, 101, 769, 108, 105, 101]
Run Code Online (Sandbox Code Playgroud)

max*_*moo 5

看一下这些问题:带有撇号的Python title()带有异常的Titlecasing字符串

基本上它看起来像内置title函数的限制,它似乎对它认为是单词边界非常自由.

你可以使用string.capwords:

import string
string.capwords('ame?lie')
Out[18]: 'Ame?lie'
Run Code Online (Sandbox Code Playgroud)

你可以做的另一件事是使用内置重音的角色é('\xc3\xa9')e:

b'am\xc3\xa9lie'.decode().title()
Out[21]: 'Amélie'
Run Code Online (Sandbox Code Playgroud)