-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_data.py
85 lines (74 loc) · 1.93 KB
/
export_data.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
import os
import csv
def get_latest(filenames):
if filenames:
filenames.sort(reverse=True)
return filenames[0]
else:
raise KeyError
def get_list_from_file(file, topic):
f_contents = []
f_contents.append(topic)
with open(file) as f:
content = f.readlines()
for line in content:
l = line.split(',', 1)[1]
if l == 'field.data\n':
continue
# print(line.strip('\n'))
f_contents.append(l.strip('\n'))
return f_contents
def main():
mypath = './src/cepheus_robot/bags'
bag_files = []
for (dirpath, dirnames, filenames) in os.walk(mypath):
for filename in filenames:
if filename.endswith('.bag'):
bag_files.append(filename)
break
try:
filename = get_latest(bag_files)
except KeyError:
return 1
f_prefix = filename.split('.', 1)[0]
txt_files = []
for (dirpath, dirnames, filenames) in os.walk(mypath):
for filename in filenames:
if filename.startswith(f_prefix) and filename.endswith('.txt'):
txt_files.append(filename)
break
filepaths = []
for txt_file in txt_files:
filepath = mypath + '/' + txt_file
# print(filepath)
filepaths.append(filepath)
# get file contents
print('Getting file contents')
f_contents = []
lengths = []
for file in filepaths:
topic = file.split('bag.', 1)[1].split('.txt', 1)[0]
# print(topic)
# if topic not in ['set_ls_qd', 'set_le_qd', 'set_re_qd']:
l = get_list_from_file(file, topic)
print(topic, len(l))
lengths.append(len(l))
f_contents.append(l)
print('Done')
lengths.sort()
min_len = lengths[0]
# print (len(f_contents))
# write to csv
csv_filename = f_prefix + '.csv'
with open(csv_filename, "w") as f:
writer = csv.writer(f)
# print(list(zip(*f_contents)))
writer.writerows(list(zip(*f_contents[-min_len:])))
# delete all txt
# for (dirpath, dirnames, filenames) in os.walk(mypath):
# for filename in filenames:
# if filename.endswith('.txt'):
# os.remove(mypath + '/' + filename)
# break
if __name__ == "__main__":
main()