-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog2csv.py
executable file
·111 lines (94 loc) · 4.16 KB
/
log2csv.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/python3
#encoding=utf-8
import sys, getopt,os
import re
import csv
def main(argv):
# default parameters
log_file_path = "$ROS_HOME/log/latest/rosout.log"
csv_file_path = "$ROS_HOME/log/latest/out.csv"
begin_partten = 'Node Startup'
end_partten = ''
data_partten = 'cur_pose_normed_std: %d,\tcur_pose_accued_std: %f,\ttraj_length: %f,\tprecent: %f\%'
enable_timestamp = True
# get user input
try:
opts, args = getopt.getopt(argv,"hi:o:b:e:d:t",["ifile=","ofile=","bp","ep","dp","time"])
except getopt.GetoptError:
print('log2csv.py \n'
'-i <input log file path, default=$ROS_HOME/log/latest/rosout.log> \n'
'-o <out csv file path, default=$ROS_HOME/log/latest/out.csv> \n'
'-b <begin_partten, default=\'Node Startup\'> \n'
'-e <end_partten, default=\'\'> \n'
'-d <data_partten, example=\'cur_pose_normed_std: %d,\\tcur_pose_accued_std: %f,\\ttraj_length: %f,\\tprecent: %f\%\'> \n'
'-t <enable_timestamp, default=True> \n')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('log2csv.py \n'
'-i <input log file path, default=$ROS_HOME/log/latest/rosout.log> \n'
'-o <out csv file path, default=$ROS_HOME/log/latest/out.csv> \n'
'-b <begin_partten, default=\'Node Startup\'> \n'
'-e <end_partten, default=\'\'> \n'
'-d <data_partten, example=\'cur_pose_normed_std: %d,\\tcur_pose_accued_std: %f,\\ttraj_length: %f,\\tprecent: %f\%\'> \n'
'-t <enable_timestamp, default=True> \n')
sys.exit()
elif opt in ("-i", "--ifile"):
log_file_path = arg
elif opt in ("-o", "--ofile"):
csv_file_path = arg
elif opt in ("-b", "--bp"):
begin_partten = arg
elif opt in ("-e", "--ep"):
end_partten = arg
elif opt in ("-d", "--dp"):
data_partten = arg
elif opt in ("-t", "--time"):
enable_timestamp = arg
log_file_path = os.path.expandvars(log_file_path)
csv_file_path = os.path.expandvars(csv_file_path)\
print('log_file_path:', log_file_path)
print('csv_file_path:', csv_file_path)
print('begin_partten:', begin_partten)
print('end_partten:', end_partten)
print('data_partten:', data_partten)
print('enable_timestamp:', enable_timestamp)
# begin convert
begin_line_num = 0
end_line_num = 0
table_head = []
for header_match in re.finditer('\w+[: =\t]', data_partten):
header_text = header_match.group()
header_text = re.match('\w+', header_text).group()
table_head.append(header_text)
row_data_len = len(table_head)
table_data = []
with open(log_file_path, "r") as file:
for line_i, line in enumerate(file, 1):
if re.search(begin_partten,line):
begin_line_num = line_i > begin_line_num and line_i or begin_line_num
if re.search(end_partten,line):
end_line_num = line_i > end_line_num and line_i or end_line_num
with open(log_file_path, "r") as file:
for line_i, line in enumerate(file, 1):
if line_i >= begin_line_num and line_i <= end_line_num and re.search(table_head[0],line):
raw_line_data = []
line_data = []
for num_match in re.finditer('(?![A-Za-z])(([-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)|([-+]?\d+))(?![A-Za-z])', line):
raw_line_data.append(num_match.group())
if enable_timestamp: line_data.append(raw_line_data[0]) #timestamp
line_data.extend(raw_line_data[-row_data_len:])
table_data.append(line_data)
with open(csv_file_path, 'w') as csvfile:
try:
writer = csv.writer(csvfile)
field_names = table_head
if enable_timestamp:
field_names.insert(0,'timestamp')
writer.writerow(field_names)
writer.writerows(table_data)
finally:
csvfile.close()
print('Convert Finished')
if __name__ == "__main__":
main(sys.argv[1:])