I have a script that I've been working on turning four individual steps(scripts) into 1. I'm down to my last problem and I know I'm over thinking it but need a bit of help to turn the corner.
I either need to figure out how to merge two files after the last IF-LOOP Statement or find some way to write directly to one text file from the script in the order needed (listed below as final).
Currently, the script shows I'm trying to merge(append) at the bottom after the IF-LOOP statements. If a more efficient way is better please shine some light for me. Just to be more clear hopefully, I have tried to write directly to one file and both if-Statement matches show up but my second da_list header repeats because it's inside the loop or doesn't show at all if I have the text header outside the loop. That's why you see the headers appearing before the if statements. Also, just so you know what the cf.txt file look like.
cf.txt example piece. Dots (periods) are one per column (A-D). Mixchar words start in column E
ABCDE
header row
. . . . 5J1SYMC2
. . . . 2TEHUOB1
. . . . TWSIHNB2
. . . . SYHNRWE2
. . . . BFHYSJF1
cf = open(r"E:\test2\cf.txt", "r")
f = open(r"E:\test2\F.txt", "r")
da = open(r"E:\test2\DA.txt","r")
output5 = open(r"E:\test2\output5.txt", "a")
output6 = open(r"E:\test2\output6.txt", "a")
list2 = f.read().split()
list3 = da.read().split()
next(cf)
newlist = []
f_list = []
da_list = []
output5.write(" F_List \n") #text header for output5
output5.write("\n")
output6.write(" Da_List \n") #text header for output6
output6.write("\n")
for line in cf: #cycles thru cf.txt column 5 removes last 2 chars_
rc = (line.split()[4][:-2]) #and append string(rc) in (newlist).
if rc not in newlist:
newlist = []
newlist.append(rc)
check = any(item in newlist for item in list2) #check string(rc) if exist in f_list_
if check is True: #clears previous f_list line and appends new.
if rc not in f_list:
f_list = []
f_list.append(f'{rc}', ),
output5.write(f'{f_list}'[2:-2]), #writes (rc) strings that exist in f_list to
output5.write('\n') #output5.txt
check = any(item in newlist for item in list3) #check string(rc) if exist in da_list_
if check is True: #clears previous da_list line and appends new.
if rc not in da_list:
da_list = []
da_list.append(f'{rc}', ),
output6.write(f'{da_list}'[2:-2]), #writes (rc) strings that exist in f_list to
output6.write('\n') #output6.txt
fin = open(r"E:\test2\output6.txt", "r") #trying to append output6.txt to output5.txt
data2 = fin.read()
fin.close()
fout = open(r"E:\test2\output5.txt", "a")
fout.write(data2)
fout.close()
Final result wanted in output5.txt file. F_list header with random mixchar words matches from (cf.txt) string and f_list. And the same for the da_list printed(appended) below.
F_List
2TEHUO
5JESYM
BFHYSJ
SYHNRW
TWSIHN
Da_List
HKHDU7
DJSKUO
DJDH8
KSIE3
SWSYR