在Python中将snake_case转换为lowerCamelCase

Viv*_*mar 1 python regex

试图用它的大写变体替换_(字符).例如:

hello_string_processing_in_python to helloStringProcessingInPython
Run Code Online (Sandbox Code Playgroud)

由于字符串在Python中是不可变的,如何有效地完成字符串替换?

ss = 'hello_string_processing_in_python'
for i in re.finditer('_(\w)', ss):
    ????
Run Code Online (Sandbox Code Playgroud)

Avi*_*Raj 6

你可以试试这个,

>>> s = "hello_string_processing_in_python"
>>> re.sub(r'_([a-z])', lambda m: m.group(1).upper(), s)
'helloStringProcessingInPython'
Run Code Online (Sandbox Code Playgroud)

  • 优雅.你可以将它的性能与循环字符串进行比较吗? (2认同)