Skip to content

Instantly share code, notes, and snippets.

@glamp
Created September 11, 2013 21:01
Show Gist options
  • Save glamp/6529725 to your computer and use it in GitHub Desktop.
Save glamp/6529725 to your computer and use it in GitHub Desktop.
def camel_to_snake(column_name):
"""
converts a string that is camelCase into snake_case
Example:
print camel_to_snake("javaLovesCamelCase")
> java_loves_camel_case
See Also:
http://stackoverflow.com/questions/1175208/elegant-python-function-to-convert-camelcase-to-camel-case
"""
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', column_name)
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment