Last active
August 29, 2015 13:57
-
-
Save EronHennessey/9580786 to your computer and use it in GitHub Desktop.
Convert CamelCase (or camelCase) to snake_case in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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