-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab2csv.py
More file actions
165 lines (129 loc) · 4.98 KB
/
Copy pathlab2csv.py
File metadata and controls
165 lines (129 loc) · 4.98 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from itertools import zip_longest
from os import getcwd
from os.path import exists
from sys import argv, exit
import logging
from webbrowser import open as wopen
from unidecode import unidecode
import PySimpleGUI as sg
import ctypes
import platform
def make_dpi_aware() -> None:
if int(platform.release().split('.')[0]) >= 8:
ctypes.windll.shcore.SetProcessDpiAwareness(True)
try:
make_dpi_aware()
except:
pass
sg.theme('Reddit')
layout = [
[
sg.Input(key='load_lab', do_not_clear=False, enable_events=True, visible=False),
sg.FileBrowse('Browse & Convert', file_types = (('LAB Files', '*.lab'), ('All files', '*')),
initial_folder=getcwd(), button_color='green'),
sg.Button('Quit', button_color='red')
],
[sg.Multiline('', size=(50, 10), key='output', disabled=True, expand_x=True, expand_y=True)],
[sg.Text("Drag'n'Drop lab file onto this exe file should work too.")],
[sg.Text('https://github.com/xtotdam/lab2csv', enable_events=True, key='github', font='Any 8', expand_x=True, justification='right', text_color='blue')],
]
def convert_infile_to_outfile(s):
return s + '.csv'
def convert_lab_to_csv(infile:str, outfile:str, printfn) -> None:
# Open & read
printfn(f"Opening {infile}", t='blue')
try:
f = open(infile, 'r', encoding='utf16')
except:
try:
f = open(infile, 'r', encoding='utf8')
except:
raise
labfile = f.read()
f.close()
# Convert UTF16 -> ASCII
labfile = unidecode(labfile, errors='replace')
# Read this weird INI format
splits = labfile.split('[')
sections = list()
for s in splits:
try:
name, contents = s.split(']')
sections.append((name, contents.strip()))
except ValueError:
logging.warning(f'Cannot split "{s[:20]}"')
printfn(f"Found {[x[0] for x in sections].count('vecteur')} columns with data")
def normalize_points(points:str) -> 'List[float]':
points = points.split('{')[-1][:-1]
points = filter(bool, points.replace('\n', ' ').replace('\t', ' ').split(' '))
points = list(map(float, points))
return points
# Parse 'vecteur' sections
# Logic:
# Single line must contain '='
# Lines delimiter '\n' -> ';\n', this allowsc to split them later by ';' and not by '\n'
# Later split and prepare for sorting
data = list()
for section in sections:
if section[0] == 'vecteur':
section_dict = dict()
contents = section[1].split('\n')
c1 = list()
for c in contents:
if '=' in c and '{' not in c:
c += ';'
c1.append(c)
c3 = ''.join(c1).replace('}', '};').split(';')
for c in c3:
try:
# print(c)
parts = [x.strip() for x in c.split('=')]
if parts[0] == 'points':
section_dict[parts[0]] = normalize_points(parts[1])
if parts[0] in ('oid', 'nom'):
section_dict[parts[0]] = parts[1].strip('"')
except (ValueError, IndexError):
printfn(f'*** Error with section. Skip: {c}')
section_dict['name'] = section_dict["nom"]
section_dict['sortname'] = (int(section_dict["oid"]), section_dict["name"])
data.append(section_dict)
columns = [x[1] for x in sorted([y['sortname'] for y in data], key=lambda x:x[0])]
final_data = dict()
for d in data:
final_data[d['name']] = d['points']
printfn('Columns:')
for c in columns:
printfn(f' {c} (length {len(final_data[c])})')
printfn(f'Writing into {outfile}', t='green')
with open(outfile, 'w') as f:
f.write(','.join(columns) + '\n')
rows = map(list, zip_longest(*[final_data[n] for n in columns], fillvalue=''))
for row in rows:
f.write(','.join(map(str,row)) + '\n')
if __name__ == '__main__':
if len(argv) > 1:
def printfn(s, **kwargs):
print(s)
infile = argv[1]
outfile = convert_infile_to_outfile(infile)
convert_lab_to_csv(infile, outfile, printfn)
exit()
window = sg.Window('lab -> csv', layout, resizable=True, finalize=True)
def printfn(s, **kwargs):
window['output'].print(''.join(map(str, s)), **kwargs)
while True:
event, values = window.read()
if event in (sg.WINDOW_CLOSED, 'Quit'):
break
if event == 'github':
wopen('https://github.com/xtotdam/lab2csv')
if event == 'load_lab':
infile = values['load_lab']
outfile = convert_infile_to_outfile(infile)
if exists(outfile):
printfn(f'Error: {outfile} already exists!', t='red')
else:
try:
convert_lab_to_csv(infile, outfile, printfn)
except Exception as e:
printfn(e)