本文主要是介绍Python re正则表达式更改为pythonic名称,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
初学python,写两个函数。
import os
import re#利用正则模块re将C++ java中的命名转换为python的命名方式
_first_cap_regex = re.compile('(.)([A-Z][a-z]+)')
_number_cap_regex = re.compile('([a-z])([0-9]{2,})')
_end_cap_regex = re.compile('([a-z0-9])([A-Z])')def pythonize_name(name):"""Convert camel case to a "pythonic" name.Examples::pythonize_name('CamelCase') -> 'camel_case'pythonize_name('already_pythonized') -> 'already_pythonized'pythonize_name('HTTPRequest') -> 'http_request'pythonize_name('HTTPStatus200Ok') -> 'http_status_200_ok'pythonize_name('UPPER') -> 'upper'pythonize_name('ContentMd5')->'content_md5'pythonize_name('') -> ''"""if name == "eTag":return "etag"s1 = _first_cap_regex.sub(r'\1_\2', name)s2 = _number_cap_regex.sub(r'\1_\2', s1)return _end_cap_regex.sub(r'\1_\2', s2).lower()#递归输出一个目录
def print_directory_contents(sPath): for sChild in os.listdir(sPath): sChildPath = os.path.join(sPath,sChild)if os.path.isdir(sChildPath):print_directory_contents(sChildPath)else:print sChildPath
二、一个py文件调用另外一个py文件中的变量
(1)
from test2 import str2
print str2(2)
import test2
print test2.str2
这篇关于Python re正则表达式更改为pythonic名称的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!