-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_data.py
More file actions
86 lines (73 loc) · 2.78 KB
/
Copy pathextract_data.py
File metadata and controls
86 lines (73 loc) · 2.78 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
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 10 20:04:57 2020
@author: Shen Ge
@name: Extract Data
@description:
Extracts data from a text file with defined markers.
Generally adaptable - nimble and small.
Main shows an example
"""
def extract_basic(identifiers,delimiter,file_name,output):
'''Extracts every line with identifiers after the identifier word.'''
i=0
with open(file_name,mode='r') as file, \
open(output,mode='w') as out_file:
string = '#,'+','.join(identifiers)+'\n'
out_file.write(string)
for fovstr in file:
for identifier in identifiers:
if fovstr.find(identifier) != -1:
i+=1
data = fovstr.split(delimiter)[1][:-1]
print(f'{i},{data}')
out_file.write(f'{i},{data}\n')
def extract_lines(identifier,delimiter,headers,numlines,file_name,output,timecalc=False):
i=0
with open(file_name,mode='r') as file, open(output,mode='w') as out_file:
string = '#,'+','.join(headers)+'\n'
out_file.write(string)
#out_file.write('#,Phase,Start,End,Duration(s)\n')
for fovstr in file:
if fovstr.find(identifier)!=-1:
i+=1
data[0]=fovstr.split(delimiter)[1][:-1]
string = f'{i},{data[0]}'
for count,dat in enumerate(data[1:]):
nextline=next(file)
dat=nextline.split(delimiter)[1][:-1]
data[count+1]=dat
string+=f',{dat}'
# if last read in line is seconds
if timecalc:
hourminsec = convert(data[-1])
print(hourminsec)
string+=f',{hourminsec}'
string+='\n'
print(string)
out_file.write(string)
### miscellaneous functions
def convert(seconds):
seconds = float(seconds)
min, sec = divmod(seconds,60)
hour, min = divmod(min,60)
day, hour = divmod(hour,24)
if day != 0:
return "%dd%d:%02d:%02d" % (day, hour, min, sec)
else:
return "%02d:%02d:%02d" % (hour, min, sec)
#%%
if __name__ == "__main__":
file_name = 'input.txt'
output = 'output.csv'
# Define the identifiers. These are words that indicate these lines need to be extracted.
identifiers = ['phase','start','end','period']
delimiter = ':'
#extract_basic(identifiers,delimiter,file_name,output)
#%%
identifier = 'phase'
delimiter = ':'
numlines = 4
headers = [identifier,'start','end','duration(s)','duration(hh:mm:ss)']
data = 4*[0]
extract_lines(identifier,delimiter,headers,numlines,file_name,output,timecalc=True)