-
Notifications
You must be signed in to change notification settings - Fork 4
/
find_possible_dates.py
66 lines (61 loc) · 2.15 KB
/
find_possible_dates.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
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 09 11:56:28 2018
@author: cmi001
"""
from __future__ import print_function
from builtins import range
import os
def find_possible_dates(str_):
"""
Finds index of possible year and month in a string if the date is of format
yyyymm or yyyy{char}mm for years between 1900 and 2020
"""
basename = os.path.basename(str_)
months =['{0:02d}'.format(i) for i in range(1,12)]
years = ['{0}'.format(i) for i in range(1900, 2020)]
options = {}
i = 0
for y in years:
index = basename.find(y)
if index > 0:
if basename[index+4:index+6] in months:
options[i] = ([index, index+4], [index+4, index+6])
i +=1
elif basename[index+5:index+7] in months:
options[i] = ([index, index+4], [index+5, index+7])
i+=1
else:
options[i] = [index, index+4]
if len(list(options.keys())) == 0:
print('Could not find datestring')
elif len(list(options.keys())) > 1:
print('Multiple possible locations for datestring')
return options[0]
def find_possible_dates_negative(str_):
"""
Finds index of possible year and month in a string if the date is of format
yyyymm or yyyy{char}mm for years between 1900 and 2020
"""
basename = os.path.basename(str_)
months =['{0:02d}'.format(i) for i in range(1,12)]
years = ['{0}'.format(i) for i in range(1900, 2020)]
options = {}
i = 0
for y in years:
index1 = basename.find(y)
index = index1 - len(basename)
if index1 > 0:
if basename[index+4:index+6] in months:
options[i] = ([index, index+4], [index+4, index+6])
i +=1
elif basename[index+5:index+7] in months:
options[i] = ([index, index+4], [index+5, index+7])
i+=1
else:
options[i] = [index, index+4]
if len(list(options.keys())) == 0:
print('Could not find datestring')
elif len(list(options.keys())) > 1:
print('Multiple possible locations for datestring')
return options[0]