-
Notifications
You must be signed in to change notification settings - Fork 0
/
UPS-Splitter.py
216 lines (144 loc) · 3.75 KB
/
UPS-Splitter.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#isa to first LX|
#LX through next CD3|
#last CD3 to end
#B307 == L305
#SE01 == | count minus 5
#Add all the L104 segments up
#L305/B307 == L104 count (add all up)
#GE01 = 1
def lxSplitter():
with open('batch.txt', 'r+') as f:
for line in f:
LXsegments = line.split('LX')
return LXsegments
def header(data):
header = data[0]
return header
def footer(data):
footer = "LX" + data[-1]
return footer
def lxFinder(data):
m = 0
LXlist = []
#Looks for tracking number, N9*CN, then pulls out the containing LX segments
#searches list of split LX segments
while m < len(data):
if '1ZV4118F036966335' in data[m]:
LXlist.append('LX' + data[m])
elif '1Z4507890372552575' in data[m]:
LXlist.append('LX' + data[m])
else:
pass
m+=1
return LXlist
def segmentsplitter(data):
segment = data.split('|')
return segment
def elementsplitter(segment):
#Split segments by elements, return them in a list
splitdata = [letter.split('~') for letter in segment]
# for letter in segment:
# splitdata.append(letter.split('~'))
return splitdata
def segmentcounter(data):
#Counts segments and then replaces SE01 with new value
for element in data:
if element[0] == 'SE':
element[1] = len(data)-5
print 'Changing SE01 to segment total: ' + str(element[1])
return data
def b3l3Checker(data):
#Checks to make sure B307 and L305 are the same
B307 = data[3][7]
L305 = data[-5][5]
if B307 == L305:
print "B307 and L305 = " + data[3][7]
else:
print "B307 and L305 don't match"
return B307
return L305
def totalSum(data):
#Totals all L104 segments and then changes B307 and L305 to that value
L1segment = [int(segment[4]) for segment in data if segment[0] == 'L1']
# for segment in data:
# if segment[0] == 'L1':
# L1segment.append(int(segment[4]))
L104 = sum(L1segment)
data[3][7] = L104
data[-5][5] = L104
print data[3]
print data[-5]
return data
def geChange(data):
#Change GE01 to 1
for element in data:
#print element[0]
if element[0] == 'GE':
element[1] = 1
print 'Changing GE01 to: ' + str(element[1])
return data
def headerSegment(data):
#Pull ISA to first, but not including LX segment
header = data[:13:]
#print 'Header is\n' + str(header)
return header
def footerSegment(data):
#Pulls last CD3 segment to the end
footer = data[-9:]
#print 'Footer is\n' + str(footer)
return footer
def fileWriter(data):
SegmentDelimiter = '|'
ElementDelimiter = '~'
outputpath = "C:/Users/bsfelde/Desktop/"
filename = 'UPS-redrop'
with open(outputpath + filename, "w") as text_file:
for segment in data:
for element in segment[:-1]:
text_file.write(str(element)+str(ElementDelimiter))
#writes tilde per element, except the last one
text_file.write(str(segment[-1]))
#appends last index to each segment
if segment[0] == 'IEA':
pass
else:
text_file.write(SegmentDelimiter)
#writes in | after each segment
#-------------------------------------
def main():
print '\n\n'
segments = lxSplitter()
t = header(segments)
z = footer(segments)
y = lxFinder(segments)
x = ''.join(y)
new_file = t + x + z
print 'Pulling invoices with erred tracking numbers...'
print '\n\n'
print new_file
print '\n\n'
segments = segmentsplitter(new_file)
dater = elementsplitter(segments)
headerSegment(dater)
footerSegment(dater)
secount = segmentcounter(dater)
print '\n\n'
geChange(dater)
#print secount
print '\n\n'
print 'Checking if B307 and L305 match...'
b3l3Checker(dater)
print '\n\n'
#L1 =
#x = TotalSum(dater)
print 'Changing B307 and L307 to L104 total'
print '\n'
totalSum(dater)
#print x
print '\n\n'
#print dater
print 'Writing new file, UPS-redrop.txt, to Desktop..'
fileWriter(dater)
print '\n\n'
if __name__ == '__main__':
main()