Skip to content

Instantly share code, notes, and snippets.

@EronHennessey
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EronHennessey/9580786 to your computer and use it in GitHub Desktop.
Save EronHennessey/9580786 to your computer and use it in GitHub Desktop.
Convert CamelCase (or camelCase) to snake_case in Python
#!/usr/bin/env python
import sys
import re
def camel2snake(text, sep=r'_'):
"""Convert *text* from CamelCase (or camelCase) into snake_case."""
rebounds = [r'([a-z])([A-Z])', r'([A-Z])([A-Z])']
underscored_text = str(text)
for b in rebounds:
underscored_text = re.sub(b, (r'\g<1>'+ sep + r'\g<2>'), underscored_text)
return underscored_text.lower()
if __name__ == "__main__":
if len(sys.argv) > 1:
print camel2snake(" ".join(sys.argv[1:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment