'How to read a .log file in Python
Can you please help me with to code that I can use to read .Log file and then change '-' separated value to different column.
The Content in the file is:
Configured/Initialized - Corrected - 03/13/22 - 07:24:26 - Ack? No - Severity: Low
Battery - Triggered - 03/13/22 - 15:59:48 - Ack? No - Severity: Low
Tension Low- not allowed - Finalized - 03/13/22 - 16:22:25 - Ack? Yes - Severity: Low
Solution 1:[1]
Using the CSV reader works but, as @jonrshapre noted in the comments below your post, Tension Low- not allowed will not split things correctly. If the "Tension Low- ..." is correct, then you can use the re module where you split the line at " - ".
UPDATE: 04-12-2022 -- added code answering data frame question in the comments posted below.
UPDATE: 04-15-2022 -- add code to address new type of option in log file.
import re
import pandas as pd
with open("file.log", "r") as f:
data = []
lines = f.readlines()
for line in lines:
# use strip() to remove "\n" from end of line
data.append(re.split(" - ",line.strip()))
for row in data:
# Check lines to catch case where 1st col is split due the re.split().
# All lines need 6 items so if 7 is found, row update needed.
if len(row) == 7:
row[0] = ' - '.join(row[0:2]) # update 1st item with re-joined text
row.pop(1) # delete 2nd item in list
# For all rows, split last two items (Ack & Severity) in the
# list and only store the 2nd part of the split
row[4] = row[4].split()[1]
row[5] = row[5].split()[1]
cols=['Type', 'Status', 'Date', 'Time', 'Ack', 'Severity']
# Create the pandas DataFrame
df = pd.DataFrame(data, columns = cols )
print(df)
The following is what `data' looks like after splitting the info from the log file:
['Configured/Initialized', 'Corrected', '03/13/22', '07:24:26', 'Ack? No', 'Severity: Low']
['Battery', 'Triggered', '03/13/22', '15:59:48', 'Ack? No', 'Severity: Low']
['Tension Low- not allowed', 'Finalized', '03/13/22', '16:22:25', 'Ack? Yes', 'Severity: Low']
['ABC Set - PQR motion', 'Triggered', '03/13/22', '07:07:28', 'No', 'Low']
The following is the data frame:
Type Status Date Time Ack Severity
0 Configured/Initialized Corrected 03/13/22 07:24:26 No Low
1 Battery Triggered 03/13/22 15:59:48 No Low
2 Tension Low- not allowed Finalized 03/13/22 16:22:25 Yes Low
3 ABC Set - PQR motion Triggered 03/13/22 07:07:28 No Low
Solution 2:[2]
just use csv with custom delimiter
import csv
file_object = open("file.log", "w")
reader = csv.reader(file_object, delimiter = "-")
for row in reader:
print row
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Dean Van Greunen |
