Question

I have a list of surnames and it isn't formatted the right way. Every name is written in cAmEl sTyLe -.- I'm trying to make it look more clean with title() method.

s = 'KroGer'
s = s.title()
print s
>Kroger

This one works fine. But when I have non-ascii letter in the name:

s = 'KRöGer'
s = s.title()
print s
>KröGer

the letter that follows this non-ascii remains in Upper-case. Even if I change the string:

s = 'KRöger'
s = s.title()
print s
>KröGer

I still get the wrong result. Why does it behave this way? How can I make this string become 'Kröger'?

Était-ce utile?

La solution 3

I finally found a way to do what I want. God bless generators:

name = 'KRöGer'
name = ' '.join(name[0].upper() + name[1:].lower() for n in name.split())
print name
>>Kröger

Autres conseils

This should be a unicode string:

>>> 'KRöger'.title()
'KröGer'
>>> u'KRöGer'.title()
u'Kröger'

Edit: A simple python script as an example:

# -- coding: utf-8 --
print 'KRöger'.title()  # 'KröGer'
print u'KRöGer'.title()  # 'Kröger'
print 'KRöger'.decode('utf-8').title()  # 'Kröger'

You could decode UTF 8 before the title:

print s.decode('utf-8').title()
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top