Skip to content

Instantly share code, notes, and snippets.

@codephillip
Created August 28, 2022 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codephillip/fa67221bca6bc9f0d76014da54e83ce5 to your computer and use it in GitHub Desktop.
Save codephillip/fa67221bca6bc9f0d76014da54e83ce5 to your computer and use it in GitHub Desktop.
import csv
import random
import re
"""
Helps us find the number in between two brackets ie (33)
"""
def find_between(text, start, end):
return re.findall(re.escape(start) + "(.*)" + re.escape(end), text)[0].strip()
"""
Reads the input file then performs the following
1. read one line at a time
2. convert lines that have UVM_INFO, UVM_WARNING, UVM_ERROR into one line
3. extract the fields "message type, full path to file, line number, time, hierarchical location, message" from the rows
4. create a csv with fields "message type, full path to file, line number, time, hierarchical location, message"
https://www.w3schools.com/python/python_file_open.asp
"""
def read_input_file():
# open the txt file in read mode
k = open("input1.txt", "r")
# create temporary array to store lines that UVM_INFO, UVM_WARNING, UVM_ERROR
rows = []
# read commands in full
for y in k:
if "UVM_INFO" in y or "UVM_WARNING" in y or "UVM_ERROR" in y:
rows.append(y)
elif rows:
rows[-1] = rows[-1] + " " + y
# extract the data from the commands using the structure
# <message type> <full path to file>(<line number>) @ <time><timeunit>: <hierarchical location> <message>
extracted_data = []
for row in rows:
try:
first_part = row.split('@')[0].strip()
last_part = row.split('@')[-1].strip().replace('\n', '')
message_type = first_part.split(' ', 1)[0]
path = first_part.split(' ', 1)[-1]
line_number = find_between(first_part, '(', ')')
time = last_part.split('ns:')[0]
hl_message = last_part.split('ns:')[-1].strip()
hl = hl_message.split(' ', 1)[0]
message = hl_message.split(' ', 1)[-1]
print(message_type, path, line_number, time, hl, message)
extracted_data.append([message_type, path, line_number, time, hl, message])
except Exception as e:
print(e)
return extracted_data
# https://www.pythontutorial.net/python-basics/python-write-csv-file/
def write_csv(data):
header = ['message type', 'full path to file', 'line number', 'time', 'hierarchical location', 'message']
with open(str(random.randint(100000, 999999)) + 'uart_monitor.csv', 'w', encoding='UTF8') as f:
writer = csv.writer(f)
# write the header
writer.writerow(header)
for row in data:
# write the data
writer.writerow(row)
def start():
data = read_input_file()
write_csv(data)
if __name__ == '__main__':
start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment