Python parses log files in xml format and pythonxml log files

Source: Internet
Author: User

Python parses log files in xml format and pythonxml log files

Good noon, since we haven't been back to the status for the Chinese New Year, we haven't shared a wave of small knowledge for a long time. Today, we 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 of xml1 = 'Catalina-exec 'xml2 = 'Catalina-exec' # keyword reqtimestamp time1 = '<timestamp> 'time2 =' </timestamp> '# keyword functionid functionid1 = '<functionid> 'functionid2 =' </functionid> '# keyword transid transid1 =' <transid> 'transid2 = '</transid>' # keyword siappid siappid1 = '< siappid> 'siappid2 = '</siappid>' # keyword userid userid1 = '<userid> 'userid2 =' </user Id> '# keyword mobnum mobnum1 =' <mobnum> 'mobnum2 = '</mobnum> '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:

Python parses log files in xml format and runs all the data successfully. Next, view the file.

Okay.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.