Elegant Python function to convert CamelCase to snake_case?



>>> convert('CamelCase')
2/23/2016 1:22:54 PM

Accepted Answer

This is pretty thorough:

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()

Works with all these (and doesn't harm already-un-cameled versions):

>>> convert('CamelCase')
>>> convert('CamelCamelCase')
>>> convert('Camel2Camel2Case')
>>> convert('getHTTPResponseCode')
>>> convert('get2HTTPResponseCode')
>>> convert('HTTPResponseCode')
>>> convert('HTTPResponseCodeXYZ')

Or if you're going to call it a zillion times, you can pre-compile the regexes:

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()

Don't forget to import the regular expression module

import re
1/21/2015 8:38:17 AM

There's an inflection library in the package index that can handle these things for you. In this case, you'd be looking for inflection.underscore():

>>> inflection.underscore('CamelCase')

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow