“Kamelfall an Snake Case Python” Code-Antworten

Python Snake Case zum Kamelfall

def convert_snake_to_camel(key: str) -> str:
    """
    Coerce snake case to camel case

	Use split with destructuring to grab first word and store rest in list.
    map over the rest of the words passing them to str.capitalize(), 
    destructure map list elements into new list with first word at front, and
    finally join them back to a str.
    
    Note: may want to use `str.title` instead of `str.capitalize`

    :param key: the word or variable to convert
    :return str: a camel cased version of the key
    """

    first, *rest = key.split('_')

    camel_key: list = [first.lower(), *map(str.capitalize, rest)]

    return ''.join(camel_key)
 
Sleepy Sandpiper

Kamelfall an Snake Case Python

import re

name = 'CamelCaseName'
name = re.sub(r'(?<!^)(?=[A-Z])', '_', name).lower()
print(name)  # camel_case_name
Doubtful Dolphin

Ähnliche Antworten wie “Kamelfall an Snake Case Python”

Fragen ähnlich wie “Kamelfall an Snake Case Python”

Weitere verwandte Antworten zu “Kamelfall an Snake Case Python” auf Python

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen