684
社区成员
发帖
与我相关
我的任务
分享
import re
for line in open('D:/t00ls/PyScripter/1.txt'):
match_uid=re.search(r'(?<=^uid=)\w+(?=,)',line)
if match_uid:
uid=match_uid.group(0)
match_password=re.search(r'(?<=^userPassword={crypt}).*',line)
if match_password:
cmd='%s:%s\n'%(uid,match_password.group(0))
#f=open('D:/t00ls/PyScripter/f.txt','a')
#f.write(cmd)
#f.close()
print cmd
# coding=UTF8
import re
def readFile(fname, result):
f = open(fname, "r")
if not f:
return False
lines = f.readlines()
f.close()
i = 0
cnt = len(lines)
pat = ['uid=(\w+),', 'userPassword=\{crypt\}(\w+)']
while i < cnt:
line = lines[i].strip(" \t\n")
i += 1
uid = None
m = re.findall(pat[0], line)
if len(m) > 0:
uid = m[0]
while uid:
line = lines[i].strip(" \t\n")
i += 1
m = re.findall(pat[1], line)
if len(m) > 0:
pwd = m[0]
result.append((uid, pwd))
uid = None
break
return True
def printArray(arr, fout):
f = open(fout, "w")
if not f:
return False
for item in arr:
#print "%s:%s" %(item[0], item[1])
f.write("%s:%s\n" %(item[0], item[1]))
f.close()
return True
if __name__ == '__main__':
fname = "test.txt"
fout = "result.log"
result = []
if readFile(fname, result):
printArray(result, fout)