1. 程式人生 > >Python指令碼--基於正則表示式對檔案進行解析

Python指令碼--基於正則表示式對檔案進行解析

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"/>