# we interweave lines from four files into a single output file # this assumes that the files are in the same directory as the program # the files are named file{1..7}.txt # the output file is named output.txt # the files are formatted as follows: # file1.txt: # E station info # E1 line 1 shef info # E2 line 2 shef info # <> import sys import os def main(argv): # set the key length - how many lines per row with / key_length = 8 # get the file names from the command line file1 = argv[1] file2 = argv[2] file3 = argv[3] file4 = argv[4] file5 = argv[5] file6 = argv[6] file7 = argv[7] # get the output file name from the command line output_file = argv[8] # read in the first file file1 = open(file1, "r") file1 = file1.read() # read in the second file file2 = open(file2, "r") file2 = file2.read() # read in the second file file3 = open(file3, "r") file3 = file3.read() # read in the second file file4 = open(file4, "r") file4 = file4.read() # read in the second file file5 = open(file5, "r") file5 = file5.read() # read in the second file file6 = open(file6, "r") file6 = file6.read() # read in the second file file7 = open(file7, "r") file7 = file7.read() # create the output dictionary output = {} header = '' # find a line that begins with ".E " in the first file print('Starting file 1') for index, line in enumerate(file1.splitlines()): if index == 0: header += str(line + "\n") elif index == 1: header += str(line + "\n") elif index == 2: header += str(line + "\n") elif index == 3: header += str(line + "\n") elif index == 4: header += str(line + "\n") elif index == 5: header += str(line + "\n") if line.startswith(".E "): #print(f'slice', line[-12:-10]) get_status = line[-12:-10] # use the line as the dictionary key key = line[:key_length] key = key + get_status # if dicitonary key is not in the dictionary, add it if key not in output: output[key] = [ line ] # find a line that begins with ".E1 " in the first file if line.startswith(".E1 "): # add the line to the dictionary using the key output[key].append(line) # find a line that begins with ".E2 " in the first file if line.startswith(".E2 "): # add the line to the dictionary using the key output[key].append(line) # find a line that begins with ".E2 " in the first file if line.startswith(".E3 "): # add the line to the dictionary using the key output[key].append(line) print('Starting file 2') # find a line that begins with ".E " in the second file for line in file2.splitlines(): if line.startswith(".E "): get_status = line[-12:-10] # use the line as the dictionary key key = line[:key_length] key = key + get_status # if dicitonary key is not in the dictionary, throw an error if key not in output: raise Exception("Key not found in file2: " + key) # find a line that begins with ".E1 " in the second file if line.startswith(".E1 "): # replace the first three characters with ".E4", only once line = line.replace(".E1", ".E4", 1) # add the line to the dictionary using the key output[key].append(line) # find a line that begins with ".E2 " in the second file if line.startswith(".E2 "): # replace the first three characters with ".E5", only once line = line.replace(".E2", ".E5", 1) # add the line to the dictionary using the key output[key].append(line) if line.startswith(".E3 "): # replace the first three characters with ".E6", only once line = line.replace(".E3", ".E6", 1) # add the line to the dictionary using the key output[key].append(line) print('Starting file 3') # find a line that begins with ".E " in the second file for line in file3.splitlines(): if line.startswith(".E "): get_status = line[-12:-10] # use the line as the dictionary key key = line[:key_length] key = key + get_status # if dicitonary key is not in the dictionary, throw an error if key not in output: raise Exception("Key not found in file3: " + key) # find a line that begins with ".E1 " in the second file if line.startswith(".E1 "): # replace the first three characters with ".E17", only once line = line.replace(".E1", ".E7", 1) # add the line to the dictionary using the key output[key].append(line) # find a line that begins with ".E2 " in the second file if line.startswith(".E2 "): # replace the first three characters with ".E8", only once line = line.replace(".E2", ".E8", 1) # add the line to the dictionary using the key output[key].append(line) if line.startswith(".E3 "): # replace the first three characters with ".E9", only once line = line.replace(".E3", ".E9", 1) # add the line to the dictionary using the key output[key].append(line) print('Starting file 4') # find a line that begins with ".E " in the second file for line in file4.splitlines(): if line.startswith(".E "): get_status = line[-12:-10] # use the line as the dictionary key key = line[:key_length] key = key + get_status # if dicitonary key is not in the dictionary, throw an error if key not in output: raise Exception("Key not found in file4: " + key) # find a line that begins with ".E1 " in the second file if line.startswith(".E1 "): # replace the first three characters with ".E10", only once line = line.replace(".E1", ".E10", 1) # add the line to the dictionary using the key output[key].append(line) # find a line that begins with ".E2 " in the second file if line.startswith(".E2 "): # replace the first three characters with ".E11", only once line = line.replace(".E2", ".E11", 1) # add the line to the dictionary using the key output[key].append(line) # find a line that begins with ".E3 " in the second file if line.startswith(".E3 "): # replace the first three characters with ".E12", only once line = line.replace(".E3", ".E12", 1) # add the line to the dictionary using the key output[key].append(line) print('Starting file 5') # find a line that begins with ".E " in the second file for line in file5.splitlines(): if line.startswith(".E "): get_status = line[-12:-10] # use the line as the dictionary key key = line[:key_length] key = key + get_status # if dicitonary key is not in the dictionary, throw an error if key not in output: raise Exception("Key not found in file5: " + key) # find a line that begins with ".E1 " in the second file if line.startswith(".E1 "): # replace the first three characters with ".E13", only once line = line.replace(".E1", ".E13", 1) # add the line to the dictionary using the key output[key].append(line) # find a line that begins with ".E2 " in the second file if line.startswith(".E2 "): # replace the first three characters with ".E14", only once line = line.replace(".E2", ".E14", 1) # add the line to the dictionary using the key output[key].append(line) # find a line that begins with ".E3 " in the second file if line.startswith(".E3 "): # replace the first three characters with ".E15", only once line = line.replace(".E3", ".E15", 1) # add the line to the dictionary using the key output[key].append(line) # find a line that begins with ".E4 " in the second file print('Starting file 6') # find a line that begins with ".E " in the second file for line in file6.splitlines(): if line.startswith(".E "): get_status = line[-12:-10] # use the line as the dictionary key key = line[:key_length] key = key + get_status # if dicitonary key is not in the dictionary, throw an error if key not in output: raise Exception("Key not found in file6: " + key) # find a line that begins with ".E1 " in the second file if line.startswith(".E1 "): # replace the first three characters with ".E16", only once line = line.replace(".E1", ".E16", 1) # add the line to the dictionary using the key output[key].append(line) # find a line that begins with ".E2 " in the second file if line.startswith(".E2 "): # replace the first three characters with ".E17", only once line = line.replace(".E2", ".E17", 1) # add the line to the dictionary using the key output[key].append(line) # find a line that begins with ".E3 " in the second file if line.startswith(".E3 "): # replace the first three characters with ".E18", only once line = line.replace(".E3", ".E18", 1) # add the line to the dictionary using the key output[key].append(line) print('Starting file 7') # find a line that begins with ".E " in the second file for line in file7.splitlines(): if line.startswith(".E "): get_status = line[-12:-10] # use the line as the dictionary key key = line[:key_length] key = key + get_status # if dicitonary key is not in the dictionary, throw an error if key not in output: raise Exception("Key not found in file7: " + key) # find a line that begins with ".E1 " in the second file if line.startswith(".E1 "): # replace the first three characters with ".E19", only once line = line.replace(".E1", ".E19", 1) # add the line to the dictionary using the key output[key].append(line) # find a line that begins with ".E2 " in the second file if line.startswith(".E2 "): # replace the first three characters with ".E20", only once line = line.replace(".E2", ".E20", 1) # add the line to the dictionary using the key output[key].append(line) print(f'Printing to file location: ', output_file) # write the output dictionary to a file with the values with open(output_file, "w") as f: first = True f.write(header) # loop through the dictionary writing all values to the file for key in output: for line in output[key]: if first: first = False f.write(line) else: f.write("\n" + line) f.close() # end of program # run the program from command line use params for input and output files if __name__ == "__main__": # check for correct number of command line arguments if not len(sys.argv) == 9: print( "Usage: python interweave.py file{1..7}.shef output.shef") sys.exit(1) main(sys.argv)