优雅的Python函数将CamelCase转换为蛇

例:

>>> convert('CamelCase')
'camel_case'

这是非常彻底的:

def convert(name):
    s1 = re.sub('(.)([A-Z][a-z]+)', r'1_2', name)
    return re.sub('([a-z0-9])([A-Z])', r'1_2', s1).lower()

适用于所有这些(并且不会损害已经非拼图的版本):

>>> convert('CamelCase')
'camel_case'
>>> convert('CamelCamelCase')
'camel_camel_case'
>>> convert('Camel2Camel2Case')
'camel2_camel2_case'
>>> convert('getHTTPResponseCode')
'get_http_response_code'
>>> convert('get2HTTPResponseCode')
'get2_http_response_code'
>>> convert('HTTPResponseCode')
'http_response_code'
>>> convert('HTTPResponseCodeXYZ')
'http_response_code_xyz'

或者,如果您打算将其称为数十亿次,则可以预编译正则表达式:

first_cap_re = re.compile('(.)([A-Z][a-z]+)')
all_cap_re = re.compile('([a-z0-9])([A-Z])')
def convert(name):
    s1 = first_cap_re.sub(r'1_2', name)
    return all_cap_re.sub(r'1_2', s1).lower()

不要忘记导入正则表达式模块

import re

包索引中有一个可以处理这些事情的变形库。 在这种情况下,你会寻找inflection.underscore()

>>> inflection.underscore('CamelCase')
'camel_case'

我不知道为什么这些都是如此复杂。

对于大多数情况下,简单的表达式([AZ]+)就可以做到这一点

>>> re.sub('([A-Z]+)', r'_1','CamelCase').lower()
'_camel_case'  
>>> re.sub('([A-Z]+)', r'_1','camelCase').lower()
'camel_case'
>>> re.sub('([A-Z]+)', r'_1','camel2Case2').lower()
'camel2_case2'
>>> re.sub('([A-Z]+)', r'_1','camelCamelCase').lower()
'camel_camel_case'
>>> re.sub('([A-Z]+)', r'_1','getHTTPResponseCode').lower()
'get_httpresponse_code'

忽略第一个字符只需添加背后(?!^)

>>> re.sub('(?!^)([A-Z]+)', r'_1','CamelCase').lower()
'camel_case'
>>> re.sub('(?!^)([A-Z]+)', r'_1','CamelCamelCase').lower()
'camel_camel_case'
>>> re.sub('(?!^)([A-Z]+)', r'_1','Camel2Camel2Case').lower()
'camel2_camel2_case'
>>> re.sub('(?!^)([A-Z]+)', r'_1','getHTTPResponseCode').lower()
'get_httpresponse_code'

如果你想将ALLCaps分离为all_caps并期望字符串中的数字,你仍然不需要做两次单独的运行,只需使用| 这个表达式((?<=[a-z0-9])[AZ]|(?!^)[AZ](?=[az]))可以处理书中的几乎所有场景

>>> a = re.compile('((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))')
>>> a.sub(r'_1', 'getHTTPResponseCode').lower()
'get_http_response_code'
>>> a.sub(r'_1', 'get2HTTPResponseCode').lower()
'get2_http_response_code'
>>> a.sub(r'_1', 'get2HTTPResponse123Code').lower()
'get2_http_response123_code'
>>> a.sub(r'_1', 'HTTPResponseCode').lower()
'http_response_code'
>>> a.sub(r'_1', 'HTTPResponseCodeXYZ').lower()
'http_response_code_xyz'

这一切都取决于你想要什么,所以使用最适合你的需求的解决方案,因为它不应该过于复杂。

的nJoy!

链接地址: http://www.djcxy.com/p/5437.html

上一篇: Elegant Python function to convert CamelCase to snake

下一篇: Convert hex string to int in Python