将Snake Case转换为更低的Camel Case(lowerCamelCase)

时间:2013-09-27 14:48:06

标签: python python-2.7

在Python 2.7中将蛇案例(my_string)转换为较低的驼峰案例(myString)会有什么好方法?

显而易见的解决方案是按下划线拆分,将除第一个单词之外的每个单词大写并重新连接在一起。

但是,我很好奇其他更惯用的解决方案或使用RegExp来实现这一点的方法(使用一些case修饰符?)

13 个答案:

答案 0 :(得分:89)

def to_camel_case(snake_str):
    components = snake_str.split('_')
    # We capitalize the first letter of each component except the first one
    # with the 'title' method and join them together.
    return components[0] + ''.join(x.title() for x in components[1:])

示例:

In [11]: to_camel_case('snake_case')
Out[11]: 'snakeCase'

答案 1 :(得分:7)

这是另一个需要,仅适用于Python 3.5:

def camel(snake_str):
    first, *others = snake_str.split('_')
    return ''.join([first.lower(), *map(str.title, others)])

答案 2 :(得分:6)

强制性单行:

import string

def to_camel_case(s):
    return s[0].lower() + string.capwords(s, sep='_').replace('_', '')[1:] if s else s

答案 3 :(得分:3)

>>> snake_case = "this_is_a_snake_case_string"
>>> l = snake_case.split("_")
>>> print l[0] + "".join(map(str.capitalize, l[1:]))
'thisIsASnakeCaseString'

答案 4 :(得分:2)

另一个班轮

def to_camel_case(snake_string):
    return snake_string.title().replace("_", "")

答案 5 :(得分:1)

根据史蒂夫的回答,这个版本应该有效:

def to_camel_case(snake_case_string):
    titleCaseVersion =  snake_case_string.title().replace("_", "")
    camelCaseVersion = titleCaseVersion[0].lower() + titleCaseVersion[1:]
    return camelCaseVersion

答案 6 :(得分:1)

以下是使用正则表达式的解决方案:

import re

def snake_to_camel(text):
    return re.sub('_([a-zA-Z0-9])', lambda m: m.group(1).upper(), text)

答案 7 :(得分:1)

对于单线粉丝..

''.join((wd.title() if i else wd) for (i,wd) in enumerate(string.split('_')))

答案 8 :(得分:0)

def to_camel_case(snake_str):
    components = snake_str.split('_')
    return reduce(lambda x, y: x + y.title(), components[1:], components[0])

答案 9 :(得分:0)

不使用列表推导:

import { createStore, combineReducers } from 'redux';
import { reducer as reduxFormReducer } from 'redux-form';

const reducer = combineReducers({
  form: reduxFormReducer, // mounted under "form"
});


const store = (window.devToolsExtension

  ? window.devToolsExtension()(createStore)
  : createStore)(reducer);

export default store;

答案 10 :(得分:0)

有点晚了,但是几天前我在/ r / python上发现了这个

pip install pyhumps

然后您可以做:

import humps

humps.camelize('jack_in_the_box')  # jackInTheBox
# or
humps.decamelize('rubyTuesdays')  # ruby_tuesdays
# or
humps.pascalize('red_robin')  # RedRobin

答案 11 :(得分:0)

因此,我需要将带有蛇形大小写参数的整个文件转换为驼峰式大小写。 Mathieu Rodic的解决方案效果最好。谢谢。

这是一个在文件上使用它的小脚本。

import re

f = open("in.txt", "r")
words = f.read()

def to_camel_case3(s):
    return re.sub(r'_([a-z])', lambda x: x.group(1).upper(), s)

f = open("out.txt", "w")
f.write(to_camel_case3(words))

答案 12 :(得分:0)

def toCamel(snake)
    return ''.join( word.capitalize()
                    for word in snake.split('_') )

允许下划线由前面的下划线转义(例如,“ Escaped__snake”将变为“ Escaped_Snake”,而“ usual_snake”将变为“ UsualSnake”。包括对空白的三元测试。

def toCamel(escaped_snake)
    return ''.join( (word.capitalize(), '_')[word=='')
                    for word in escaped_snake.split('_') )

不要大写第一段(即“ tHERE_is_a_snake”变成“ thereIsASnake”)

def toCamel(esCAPed_snake)
    words = esCAPed_snake.split('_')
    return words[0].lower() + ''.join( (word.capitalize(), '_')[word=='')
                                       for word in words[1:] )