-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_analysis.py
More file actions
78 lines (73 loc) · 2.8 KB
/
log_analysis.py
File metadata and controls
78 lines (73 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/python3
#
from dataclasses import dataclass
import csv
""" Class contains basic parameters
dataclass
class phy_values:
voltage = float
temprature = int
power = float
"""
def truncate_after_keyword(text, keyword):
# Find the starting index of the keyword
try:
index = text.find(keyword)
if index != -1:
# Slice the string from the keyword's starting index to the end
truncated_text = text[index:]
return truncated_text
else:
# Return original text or handle case where keyword isn't found
return text
except ValueError:
# Handle potential errors (though find() returns -1 on failure)
return text
def filter_string(target_string, keywords_to_keep):
# Check if ANY of the keywords are in the target string
if any(keyword in target_string for keyword in keywords_to_keep):
return target_string
else:
# Exclude (or handle as needed) the string if no keywords match
return None
try:
data_list = []
with open ("phydata.log", "r") as fin:
for line in fin:
non_empty_lines = filter(str.strip, fin)
#with open("physical_data.csv", "w+") as fin:
title_string = "Voltage, Temp, Power\n"
#fin.write(title_string)
for line in non_empty_lines:
store_data = {}
keywords = ["Voltage", "Power"]
#print(line.strip())
real_line = filter_string(line.strip(), keywords)
#print(real_line)
physical_data = truncate_after_keyword(real_line, "Voltage").split()
#print(physical_data)
for i in physical_data:
value = (i.split("="))
#print(value)
#fin.write(value[1])
#fin.write(", ")
value_dict = dict((value,))
store_data.update(value_dict)
#print(store_data)
#print(store_data)
data_list.append(store_data)
#fin.write("\n")
#fin.close()
print(data_list)
with open("new-phydata.csv", "w+") as finput:
# Define the field names (column headers). The order of fieldnames determines the column order in the CSV
data_field=["Voltage", "Temp", "Power"]
dict_write = csv.DictWriter(finput, fieldnames=data_field)
## Wreite fields rows
dict_write.writeheader()
## Wreite data rows
dict_write.writerows(data_list)
print("Finish writing data into the csv file")
finput.close()
except Exception as e:
print("Something goes wrong => ", e)