Skip to content

Instantly share code, notes, and snippets.

@Suzhou65
Created August 20, 2021 07:23
Show Gist options
  • Save Suzhou65/aff8787e7dfc6f30a3820d68f9c27f6d to your computer and use it in GitHub Desktop.
Save Suzhou65/aff8787e7dfc6f30a3820d68f9c27f6d to your computer and use it in GitHub Desktop.
Processing Logfile

Using

#Import pandas
import pandas

def read_logfile(input_filename):
    try:
        #Trans to pandas dataframe
        #Header 'E-Series' is contain extra
        header = ["IP","TIME","ACTION","CONTENT","E1","E2","E3","E4","E5","E6","E7","E8","E9","E10","E11","EX12"]
        log_file = pandas.read_csv(input_filename, encoding="utf-8", header=None, sep="[[\]]", names=header)
        
        #Merge
        log_file["CONTENTS"] = log_file[log_file.columns[3:]].apply(lambda x: " ".join(x.dropna().astype(str)), axis=1)
        
        #Drop
        log_file= log_file.drop(columns=["CONTENT","E1","E2","E3","E4","E5","E6","E7","E8","E9","E10","E11","EX12"])
        
        #Filename
        output_filename = input_filename.replace("txt","csv").replace("_audit","").replace("_ht","")
        
        #Save
        #Using 'utf_8_sig' if you want to using Excel import data from a text file function.
        log_file.to_csv(output_filename, encoding="utf_8_sig", index=False)
        
        return True
    #Error handling 
    except FileNotFoundError:
        print([input_filename, "SKip"])
    except Exception as error:
        print([input_filename, error])

Using

input_filename = input()
read_logfile(input_filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment