Created
July 8, 2017 10:09
-
-
Save geekKeen/3af1200b8132c70839912f09803643fc to your computer and use it in GitHub Desktop.
Code Collections
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
import re | |
_camelcase_re = re.compile(r'([A-Z]+)(?=[a-z0-9])') | |
def camel_to_snake_case(name): | |
def _join(match): | |
word = match.group() | |
if len(word) > 1: | |
return ('_%s_%s' % (word[:-1], word[-1])).lower() | |
return '_' + word.lower() | |
return _camelcase_re.sub(_join, name).lstrip('_') | |
if __name__ == "__main__": | |
name = "HTMLparser" | |
print camel_to_snake_case(name) # html_parser |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Python
re
模块的使用 将驼峰命名转为Python的命名方式Questions:
(?=...)
代表什么含义?_
开头, 结果又去掉?