gpt4 book ai didi

python - 无法将 'int' 对象隐式转换为 str。 Python错误

转载 作者:行者123 更新时间:2023-12-01 08:22:13 25 4
gpt4 key购买 nike

这是我的代码,尝试在出现“-”或“_”时将句子转换为驼峰式大小写。

def to_camel_case(text):
for i in text:
if text[0].isupper():
text[0] = text[0].upper()
elif i == '_' or i == '-':
text[i] = text[i].upper()
return text

执行代码时会出现上述错误。我知道错误位于 text[i] = text[i].upper() 行中的某个位置,但我无法弄清楚。谢谢。

最佳答案

IIUC 您可以使用 string.title,将 -_ 替换为 re.sub 的空格:

import re
s = "hello_world"

re.sub('_|-',' ',s).title()
# 'Hello World'

关于python - 无法将 'int' 对象隐式转换为 str。 Python错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54555720/

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