forked from cmc-ucl/MAOA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParsingSupport.py
61 lines (44 loc) · 1.36 KB
/
ParsingSupport.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
# comment
import re
def find_pattern_with_last_word(tarfile,pattern):
'''
fine pattern in a text
'''
#print(tarfile,pattern)
total_lnumber = 0
# Compile the regular expression pattern
pattern = re.compile(pattern)
with open(tarfile,'r') as file:
lines = file.readlines()
total_lnumber = len(lines)
matches = []
for lnumber, line in enumerate(lines):
#match = re.search(pattern,line)
match = pattern.search(line)
if match:
last_word = line.strip().split()[-1]
matches.append([lnumber+1,last_word])
return total_lnumber,matches
def get_lines(tarfile,line_start,line_end):
'''
line arugments human indexing convention
'''
with open(tarfile,'r') as file:
lines = file.readlines()
return lines[line_start-1:line_end-1]
def time_to_seconds(time_str):
parts = time_str.split(':')
hours = int(parts[0])
minutes = int(parts[1])
seconds = float(parts[2])
total_seconds = hours * 3600 + minutes * 60 + seconds
return total_seconds
if __name__ == '__main__':
#/Users/woongkyujee/Desktop/Python/FHI22_samples/runs/run_1/FHIaims.out 'Begin self-consistency iteration #'
file_path = "/Users/woongkyujee/Desktop/Python/FHI22_samples/runs/run_1/FHIaims.out"
pattern = 'Begin self-consistency iteration #'
matches = find_pattern_with_last_word(file_path,pattern)
print(matches)
holder = get_lines(file_path,1,15)
for item in holder:
print(item)