Python脚本--基于正则表达式对文件进行解析

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37280790/article/details/77528644

Python脚本--基于正则表达式对文件进行解析 

   首先需要了解正则表达式中的相应指令(compile、findall),我对此的理解是compile相当于一个规则的制定者,将匹配的规则制定出来,后续具体的操作是findall的工作【选择适当的规则进行匹配工作】。
   其次就是文件的读写工作【read、write】

1:首先对一行数据进行解析
import re  
#文本所在TXT文件  
file = 'ckj.txt'   
time1 = 'File Name='  
siappid1='Version='  
userid1='Size='
cc='/>'
f = open(file,'r')  
buff = f.read()  
pat = re.compile(time1+'(.*?)'+siappid1)  #.*?指匹配中间任意个字符,离最后一个标志一个或2个字符
pat1 = re.compile(siappid1+'(.*?)'+userid1)  
pat2 = re.compile(userid1+'(.*?)'+cc)  
x = open("logfx.txt", 'w')  
x.write("===========================开始数据================================="+"\n")  
x.write("\t"+"File Name"+"\t"+"Version"+"\t\t"+"Size"+"\n")
result=pat.findall(buff)
print(result)
result1=pat1.findall(buff)  
result2=pat2.findall(buff)
print(result2)
if len(result)==0:  
    result.append("空")  
if len(result1)==0:  
    result1.append("空")  
if len(result2)==0:  
    result2.append("空")  
x.write(result[0]+"\t"+result1[0]+"\t"+result2[0]+"\n") #读写不能直接操作列表,但可以是列表中的一个元素
x.write("===========================结束数据================================="+"\n")      
print("执行完毕!生成文件logfx.txt")  
x.close() 
2:对多行文件进行解析操作

import re  
#文本所在TXT文件  
file = 'ck.txt'   
time1 = 'File Name='  
siappid1='Version='  
userid1='Size='
IsPE1 ='IsPE='
MD51 = 'MD5='
cc='/>'
aa='<'
f = open(file,'r')  
buff = f.read()  
pat = re.compile(time1+'(.*?)'+IsPE1)  
pat1 = re.compile(IsPE1+'(.*?)'+siappid1)  
pat2 = re.compile(siappid1+'(.*?)'+userid1)
pat3 = re.compile(aa+'(.*?)'+cc)
result6=pat3.findall(buff)
part4=re.compile(userid1+'(.*?)'+MD51)
part5=re.compile(MD51+'(.*?)'+cc)
x = open("logfx01.txt", 'w')  
x.write("===========================开始数据================================="+"\n")  
x.write("\t"+"File Name"+"\t"+"IsPE"+"\t"+"Version"+"\t"+"Size"+"\t"+"MD5"+"\n")
for i in range(0,len(result6)): 
    result=pat.findall(result6[i])
   # print(result)
    result1=pat1.findall(result6[i])  
    result2=pat2.findall(result6[i])
   # print(result2)
    result3=part4.findall(result6[i])
    result4=part5.findall(result6[i])
    if len(result)==0:  
        result.append("空")  
    if len(result1)==0:  
        result1.append("空")  
    if len(result2)==0:  
        result2.append("空")
    if len(result4)==0:  
        result4.append("空")
    x.write(result[0]+"\t"+result1[0]+"\t"+result2[0]+"\t"+result3[0]+"\t"+result4[0]+"\n")  
x.write("===========================结束数据================================="+"\n")      
print("执行完毕!生成文件logfx.txt")  
x.close() 

具体操作文件内容:
ck.txt:文件中内容
<File Name="360AppLoader.exe" IsPE="true" Version="7.5.0.1320" Size="432224" MD5="DC5402AC867F2CB1AA5AADF1F9217900"/>
<File Name="360Base.dll" IsPE="true" Version="1.0.0.1155" Size="922024" MD5="A73CF0457DF35FAB74EF3393D2766667"/>
<File Name="360bps.dat" IsPE="false" Version="1.0.0.1043" Size="876" MD5="98CB8F3B0A344E25F870B8E3DF7EC197"/>
<File Name="360Common.dll" IsPE="true" Version="11.0.0.3403" Size="403040" MD5="FBFF07454E17B1BCEB77D0F61F2CB4E2"/>
<File Name="360Conf.dll" IsPE="true" Version="1.0.0.1016" Size="270152" MD5="F92E084DE6BF6D4CA79271EBDECDAC75"/>
ckj.txt:文件内容
 <File Name="360AppLoader.exe" Version="7.5.0.1320"Size="432224"/>
 



猜你喜欢

转载自blog.csdn.net/m0_37280790/article/details/77528644