场景:
1.某种情况需要提取XLUE里的图片.如果量大的话对照XLUE里的一个个copy出来很费时。
2.可以用python快速提取图片.这种速度是C++,Java不能比拟的.
文件1: test.xml
文件2:test_python_re.py
#! encoding=utf-8
# 导入正则包
import re
import os
import sys
import shutil
def printBitmap(xmlPath):
f = open(xmlPath)
line = f.readline()
mode = '\"[^\"]+.png\"|'
pattern = re.compile(mode) #预编译匹配规则
modeDirName = ""
patternDirName = re.compile(modeDirName);
#decode为unicode字符串,为了能创建中文路径.
picDir = "face/全局/".decode("utf-8")
try:
os.makedirs(picDir)
except Exception, e:
pass
while line:
#print line,
strMatch = pattern.findall(line) #查找所有匹配字符串
size = len(strMatch)
for i in range(0,size,1):
one = strMatch[i]
print one
one = one.replace("\\","/");
one = one.replace("\"","");
dirMatch = patternDirName.match(one);
try:
if(dirMatch):
picDir = "face/"+dirMatch.group(1)+"/" #获取分组,分组从1开始,0的话是全匹配的字符串.
print picDir
picDir = picDir.decode("utf-8")
os.mkdir(picDir)
shutil.copy(one,picDir);
except Exception, e:
pass
line = f.readline()
f.close()
if __name__=='__main__':
printBitmap("test.xml")
输出:
---------- Run ----------
"bitmap\pup.png"
face/Mask/
"bitmap/mask.btn.png"
face/BkgCtrl/
"bitmap\bkg.light.left.png"
"bitmap\bkg.light.right.png"
Output completed (0 sec consumed) - Normal Termination