如何在Python中大写包含非ASCII字符的单词?有没有办法调整stringcapitalize()方法来做到这一点?

最佳答案

使用Unicode字符串:

# coding: cp1252
print u"é".capitalize()
# Prints É

如果您只有8位字符串,请先将其解码为Unicode:
# coding: cp1252
print "é".decode('cp1252').capitalize()
# Prints É

如果然后再次需要将其作为8位字符串,请对其进行编码:
# coding: cp1252
print "é".decode('cp1252').capitalize().encode('cp1252')
# Prints É (assuming your terminal is happy to receive cp1252)

10-04 11:44