-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_edifact.py
More file actions
130 lines (107 loc) · 3.16 KB
/
Copy pathparse_edifact.py
File metadata and controls
130 lines (107 loc) · 3.16 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
from collections import namedtuple
import json
def parse_prd(prd_line):
elements = prd_line.split('+')
company_id = elements[-1]
fes = elements[1].split(':')
fes_length = len(fes)
if fes_length < 4:
fes += [None] * (4 - fes_length)
return {
'company_id': company_id,
'train_service_id': fes[0],
'service_character': fes[1],
'pricing_category': fes[2],
'item_description_code': fes[3]
}
def parse_pop(pop_line):
elements = pop_line.split('+')
components = elements[1].split(':')
dates = components[1].split('/')
start_date = dates[0]
end_date = dates[1]
return {
'start_date': start_date,
'end_date': end_date,
'period_days': components[3]
}
def parse_als(als_line):
elements = als_line.split('+')
location_type = elements[1]
components = elements[2].split(':')
location_id = components[0]
name = components[1]
longitude = elements[3]
latitude = elements[4]
return {
'location_id': location_id,
'location_type': location_type,
'name': name,
'longitude': longitude,
'latitude': latitude
}
def parse_por(por_line):
elements = por_line.split('+')
location_id = elements[1]
time = elements[2].split('*')
if len(time) == 2:
arrival_time = time[0]
departure_time = time[1]
else:
arrival_time = elements[2]
departure_time = ''
return {
'location_id': location_id,
'arrival_time': arrival_time,
'departure_time': departure_time
}
def parse_pdt(pdt_line):
elements = pdt_line.split('+')
components = elements[2].split(':')
train_type = components[3]
return {
'train_type': train_type
}
def parse_segment(lines):
global output
list_por = []
if lines == []:
return None
if lines[0][:3] != 'PRD':
raise Exception('First line of segment is not PRD')
pop, prd, start_date, end_date = {}, {}, None, None
pdt = {'train_type': None}
train_service_id = None
for line in lines:
if line[:3] == 'PRD':
prd = parse_prd(line)
train_service_id = prd['train_service_id']
if line[:3] == 'POP':
pop = parse_pop(line)
start_date = pop['start_date']
end_date = pop['end_date']
if line[:3] == 'PDT':
pdt = parse_pdt(line)
if line[:3] == 'POR':
por = parse_por(line)
list_por.append(por)
if pdt == {}:
print('PDT IS NONE!', train_service_id)
output[train_service_id + '_' + start_date + ' ' + end_date] = {
'train_service': {**prd, **pop, **pdt},
'train_timetable': list_por
}
if __name__ == '__main__':
output = {}
segment = []
with open('SKDUPD.20160624082533_3.r') as f:
for line in f:
if line[:3] == 'HDR':
break
for line in f:
if line[:3] == 'PRD':
parse_segment(segment)
segment = [line.rstrip()]
if line[:3] != 'PRD':
segment.append(line.rstrip())
# print(json.dumps(output))