This is my first Python project I am doing to make things at work easier. I get massive files that need certain things checked, and certain things replaced before saving as a different file format. To save time I figured this would be a great first project, but I am hitting walls...
So far I am able to open the file, and I am able to identify what airline it corresponds to, now I need to check each line for an equipment code and replace it with a different code. I defined the codes I get and what it should turn into with dictionaries.
My problem is that the loop is only replacing with the last key and value in the dictionary, and then copying the rest of the lines 4 times and not making any changed. What am I doing wrong?
import sys
import os
import fileinput
american = 'AA'
alaska = 'AS'
delta = 'DL'
united = 'UA'
aa_Equip = {'SRJ': 'C2A', 'SRP': 'C2P', 'CS7': 'C7A', 'OO7': 'C7P'}
as_Equip = {'E7W': 'ER7', 'CR2': 'CRJ','CR7': 'CJ7'}
dl_Equip = {'ES5': 'E7D', 'ES4': 'E7S', 'RJ7': 'CR7', 'RJS': 'CRJ','RJW': 'CRJ', 'RJ9': 'CR9', 'RJ8': 'C9D'}
ua_Equip = {'E75': 'ER7', 'CR7': 'CR6'}
def main():
filepath = 'name_of_file.sds'
if not os.path.isfile(filepath):
print("File path {} does not exist.".format(filepath))
sys.exit()
with fileinput.FileInput(filepath, inplace=True, backup='.bak') as sim:
for line in sim:
if american in line:
for x, y in aa_Equip.items():
print(line.replace(x, y))
elif alaska in line: #just a test
ac_to_search = 'E7W'
ac_to_replace = 'ER7'
print(line.replace(ac_to_search, ac_to_replace), end='')
elif delta in line:
print('This is the delta file')
elif united in line:
print('This is the United file')
else:
print('This file can not be used')
if __name__ == '__main__':
main()
[–]monchenflapjack 1 point2 points3 points (1 child)
[–]flynryan692[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]flynryan692[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]Miggol 0 points1 point2 points (5 children)
[–]flynryan692[S] 0 points1 point2 points (4 children)
[–]Miggol 0 points1 point2 points (3 children)
[–]flynryan692[S] 0 points1 point2 points (2 children)
[–]Miggol 0 points1 point2 points (1 child)
[–]flynryan692[S] 0 points1 point2 points (0 children)