Parse log files in xml format using python

Source: Internet
Author: User
This article mainly introduces how to parse log files based on xml format in python, which has some reference value. if you are interested, please refer to the good afternoon, since the Chinese New Year has not been back to the status, I haven't shared a small wave of knowledge for a long time. today, I will continue to share with you a wave of small scripts for parsing logs using Python.

First, let's see what the log looks like.

They are all in xml format. are you dizzy when you look at them ?? It's okay. let's analyze a wave first.

1. each segment starts with catalina-exec, so we will divide it by catalina-exec. after dividing it, they will all be a segment.

2. then, we will divide the keywords in the segments that we have already divided, and find out the keywords you want to split. because they are in xml format, the next work will be simple. they will all be at the beginning and end.

3. but there is another problem. There may be some keywords that you don't want, so you have to judge that if this field does not exist, I will set this field to null.

With clear thinking, the code is naturally simple.

Next let's look at the code

# Coding: UTF-8 import re # file of the TXT file where the text is located = 'iag _ interface. log' # Split a segment xml1 = 'catalina-exec 'xml2 = 'catalina-exec' # keyword reqtimestamp time1 ='
 
  
'Time2 ='
 '# Keyword functionid functionid1 ='
 
  
'Functionid2 ='
 '# Keyword transid transid1 ='
 
  
'Transid2 ='
 '# Keyword siappid siappid1 ='
 
  
'Siappid2 ='
 '# Keyword userid userid1 ='
 
  
'Userid2 ='
 '# Keyword mobnum mobnum1 ='
 
  
'Mobnum2 ='
 'F = open (file, 'R', encoding = 'utf-8') # f = open (file, 'r') # for (num, value) in enumerate (f): # print ("line number", num, "is:", value) buff = f. read () # clear the line break. Please cancel the next line of comment # buff = buff. replace ('\ n', '') pat = re. compile (time1 + '(. *?) '+ Time2, re. S) pat1 = re. compile (functionid1 + '(.*?) '+ Functionid2, re. S) pat2 = re. compile (transid1 + '(.*?) '+ Transid2, re. S) pat3 = re. compile (siappid1 + '(.*?) '+ Siappid2, re. S) pat4 = re. compile (userid1 + '(.*?) '+ Userid2, re. S) pat5 = re. compile (mobnum1 + '(.*?) '+ Mobnum2, re. S) pat6 = re. compile (xml1 + '(.*?) '+ Xml2, re. s) result6 = pat6.findall (buff) print (len (result6) x = open ("logfx.txt", 'w') x. write ("======================== start data ========== ======================================== "+" \ n ") x. write ("time" + "\ t" + "functionid" + "\ t" + "transid" + "\ t" + "siappid" + "\ t" + "userid ""+" \ t "+" mobnum "+" \ n ") for I in range (0, len (result6): result = pat. findall (result6 [I]) result1 = pat1.findall (result6 [I]) result2 = pat2.findall (result6 [I]) Result3 = pat3.findall (result6 [I]) result4 = pat4.findall (result6 [I]) result5 = pat5.findall (result6 [I]) if len (result) = 0: result. append ("null") if len (result1) = 0: result1.append ("null") if len (result2) = 0: result2.append ("null ") if len (result3) = 0: result3.append ("null") if len (result4) = 0: result4.append ("null") if len (result5) = 0: result5.append ("null") # print (result [0], "=", result1 [0], "=", result2 [0], "=", resul T3 [0], "=", result4 [0], "=", result5 [0]) x. write ("timestamp: "+ result [0] +" \ t "+ result1 [0] +" \ t "+ result2 [0] +" \ t "+ result3 [0] +" \ t "+ result4 [0] +" \ t "+" mobnum: "+ result5 [0] +" \ n ") x. write ("====================== end data ========== ======================================== "+" \ n ") print ("execution completed! Generated file logfx.txt ") x. close ()

Run the following code:

Okay.

The above is all the content of this article. I hope it will help you learn and support PHP.

For more articles about parsing log files in xml format using python, refer to the PHP Chinese website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.