How to capitalize words containing non-ASCII characters in Python? Is there a way to tune string
's capitalize()
method to do that?
views:
332answers:
2
+7
A:
Use Unicode strings:
# coding: cp1252
print u"é".capitalize()
# Prints É
If all you have is an 8-bit string, decode it into Unicode first:
# coding: cp1252
print "é".decode('cp1252').capitalize()
# Prints É
If you then need it as an 8-bit string again, encode it:
# coding: cp1252
print "é".decode('cp1252').capitalize().encode('cp1252')
# Prints É (assuming your terminal is happy to receive cp1252)
RichieHindle
2009-06-17 11:30:16
http://unicode.org/Public/UNIDATA/SpecialCasing.txt says that it is not that simple even when locale is not under consideration
J.F. Sebastian
2009-06-17 12:45:02
A:
Hank Gay
2009-06-17 11:32:52