作为python菜鸟级选手,写这么个小程序耗费了我将近两个晚上,每个功能都是先去网上search一下,从如何写python文件、如何使用库,到如何定义函数等等,没有系统看过一遍书,用起来果然费尽。写的初衷是因为mm有个笔试题是这样的。。。,结果我还没写出来,伊早就交了(用php)
python 处女作,不管多烂,帖上,日后来看看
其间饱受liangqing mm的点拨和照顾,再次感谢一哈
#递归替换
def replaceimport(i, directory, filename):
print i
i = i + 1
f = open(directory + filename)
content = f.read()
result = content
matches = re.findall(r'@import\s+"[^"]+"\s*;', content)
for match in matches:
print match
dirfile = re.compile(r'"([^/]+/)([^/]+)"').search(match)
ndir = dirfile.group(1)
nfile = dirfile.group(2)
print ndir + ',' + nfile
replace = replaceimport(i, directory + ndir, nfile)
result = content.replace(match, replace)
#result = re.sub(match, replace, content) 这货我没有开始弄清楚导致我盲目使用表达式结果造成\的灾难,出现invalid group reference的错误,后来发现replace即可
content = result
#print result
return result
python 处女作,不管多烂,帖上,日后来看看
其间饱受liangqing mm的点拨和照顾,再次感谢一哈
#递归替换
def replaceimport(i, directory, filename):
print i
i = i + 1
f = open(directory + filename)
content = f.read()
result = content
matches = re.findall(r'@import\s+"[^"]+"\s*;', content)
for match in matches:
print match
dirfile = re.compile(r'"([^/]+/)([^/]+)"').search(match)
ndir = dirfile.group(1)
nfile = dirfile.group(2)
print ndir + ',' + nfile
replace = replaceimport(i, directory + ndir, nfile)
result = content.replace(match, replace)
#result = re.sub(match, replace, content) 这货我没有开始弄清楚导致我盲目使用表达式结果造成\的灾难,出现invalid group reference的错误,后来发现replace即可
content = result
#print result
return result
--
BlogSpot: http://xusulong.blogspot.com Twitter: http://twitter.com/econsh
No comments:
Post a Comment