-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcode4.py
More file actions
37 lines (35 loc) · 960 Bytes
/
code4.py
File metadata and controls
37 lines (35 loc) · 960 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# -*- coding:utf-8 -*-
"""
将驼峰命名法字符串转换成下划线命名法
eg. GetItem ---> get_item
getIT ----> get_it
"""
def changeStr(param):
rest = []
for k,v in enumerate(param):
if v.isupper():
rest.append(k)
print rest
nrest = len(rest)
nparam = len(param)
res = ""
if rest[0] == 0:
for i in range(0,len(rest)-1):
res += param[rest[i]:rest[i+1]]
if rest[i+1] - rest[i] == 1:
continue
res += "_"
res += param[rest[nrest-1]:nparam]
else:
res += param[0:rest[0]]
res += "_"
for i in range(0,len(rest)-1):
res += param[rest[i]:rest[i+1]]
if rest[i+1] - rest[i] == 1:
continue
res += "_"
res += param[rest[nrest-1]:nparam]
return res.lower()
if __name__ == "__main__":
print changeStr("heGetItEmQY")
print changeStr("GetItem")