Skip to content

Instantly share code, notes, and snippets.

@mmas
Last active June 15, 2016 20:08
Show Gist options
  • Save mmas/1534f22e093dc93af906134e61d61241 to your computer and use it in GitHub Desktop.
Save mmas/1534f22e093dc93af906134e61d61241 to your computer and use it in GitHub Desktop.
Camelcased and dashed text to underscored text in Python
import re
def underscorize(x):
"""
Camelcased and dashed text to underscored text.
Examples:
>>> underscorize('CamelCasedText')
'camel_cased_text'
>>> underscorize('camel-cased-text')
'camel_cased_text'
>>> underscorize('Camel-Cased-Text')
'camel_cased_text'
"""
return re.sub(r'([^_])([A-Z])', r'\1_\2', re.sub(r'-', '_', x)).lower()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment