str = 'UserName' ''.join(chr(i) if i >= 97 else '_'+chr(i+32) for i in [ord(x) for x in str])[1:]
qdvictory
2013-08-07 15:16:51 +08:00
再加个开头不是大写的。。 str = 'UserName' prefix = '_' text = ''.join(chr(i) if i >= 97 else prefix+chr(i+32) for i in [ord(x) for x in str]) text[len(prefix):] if text.startswith(prefix) else text
qdvictory
2013-08-07 15:36:08 +08:00
import re str = 'UserName' r = re.compile(r'[A-Z][a-z]+') li = re.findall(r,str) '_'.join([x.lower() for x in li])