gpt4 book ai didi

python - 大写不适用于特殊字符 - Python

转载 作者:太空宇宙 更新时间:2023-11-03 13:16:40 25 4
gpt4 key购买 nike

我一直在尝试将大量字符串大写,其中一些以 utf-8 字符开头。问题是,他们没有大写!

mystring = 'lucas'
mystring.capitalize() # returns 'Lucas'

mytring = 'æthelred'
mystring.capitalize() # returns 'æthelred'

与包含 `´^¨ 和字符 ð、þ 等的元音相同。我该怎么做才能解决这个问题?

我实际上无权访问该字符串,我在其他地方获取它们,在一个文本文件中...

最佳答案

你省略了 u 。需要将字符串定义为 python 的 unicode!

>>> mytring = u"æthelred"
>>> print mytring.capitalize()
Æthelred

python 3 中字符串默认是 unicode 你不需要 u

>>> "æthelred".capitalize()
'Æthelred'

关于python - 大写不适用于特殊字符 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27723685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com