Skip to content

Instantly share code, notes, and snippets.

@nara-l
Created December 2, 2017 16:44
Show Gist options
  • Save nara-l/f472bc2e97029fbd85b6f0721bf57240 to your computer and use it in GitHub Desktop.
Save nara-l/f472bc2e97029fbd85b6f0721bf57240 to your computer and use it in GitHub Desktop.
Convert to Snake case Python
# convert to snake case, added replace - could do better with re https://stackoverflow.com/a/1176023/851056
def convert(name):
new_name = name.replace(" ", "").replace("'","")
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', new_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