1

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
Braiam
  • 1
  • 11
  • 47
  • 78
Boomer
  • 229
  • 1
  • 9
  • It would be easier to suggest improvements to your solution if you did refactor and named the variables with easy to understand the name. Also, try to remove the code that is not relevant for the question to make it easier to understand what the issue you are having is. – Fredrik Jun 17 '20 at 19:59

1 Answers1

0

One solution could be to just append the file to the second file. You should consider using variables for the file paths instead of copy-paste. Also, the name of the files is a bit confusing.

output5.close()
output6.close()
with open(r"E:\test2\output5.txt", 'a') as output5:
   with open(r"E:\test2\output6.txt", "r") as output6:
      output5.write(output6.read())

Relevant question about appending files

Here is a explanation of how you can use the with statement

Also, instead of writing the content directly to the files in the if statement, why not just store it into strings? Then in the end you could write it to a file.

If you try to read from a file, before closing it, the text won't show up. You can try the snippet below.

output5 = open(r"Test", "w")
output5.write("Foo")
output6 = open(r"Test", "r")
print(output6.read())
Fredrik
  • 484
  • 4
  • 13
  • Thanks Fredrik for answering back. All good points and will make variable & path changes moving forward. I appreciate it. However, my question now is where can I apply or place your code where it will get the results? I did paste it outside of the last loop (at bottom) and deleted my attempt to append file. It did not work. – Boomer Jun 17 '20 at 20:29
  • It's kinda funny but in vba (meaning excel vba, not the real stuff) I can put in a If statement loop and F8(step thru) the code and see code cycle the if statement, complete, and move on to next line under the if statement. Does Python not work like this? I'm thinking after my last if-statement, the code line 46 - output6.write('\n'), it should drop to next line and start new series of code. What am I missing? – Boomer Jun 17 '20 at 20:30
  • If you are talking about a debugger, there are debuggers to do that in python code also where you step line by line. Might be that you are using an IDE to write excel, VBA and a text editior for Python? – Fredrik Jun 17 '20 at 20:36
  • What was the issue when you placed it outside the last loop? Was the appending not working to the file? – Fredrik Jun 17 '20 at 20:38
  • Yeah, I have been using pycharm. Shift F9 start debugger. F8 to step. I feel like I'm not asking the right question. I understand what your code is achieving but if I place the code too high (above if statements) then the if-statement variables do not print. if I place your code below the last if-statement (slide all the way left) outside the if block, Line 48 (left justified), the if-statement variables print in both txt files but do not merge. Or, output6.txt does not append to output5.txt. I hope this make sense. this is driving me crazy. – Boomer Jun 17 '20 at 20:49
  • Sorry, I missed one of your ?'s. Line 46 is the last If-statement output6.write('\n'). I placed your code at Line 48 and erased my fin = open stuff. Result was output5.txt printed fine. Output6.txt printed fine. but nothing merged. I placed your code on Line 47 inside the last if-statement. Results, I lose strings(mixchar words) in both text files. – Boomer Jun 17 '20 at 21:00
  • I have updated the answer with closing the files first. When not using **with**, you need to close the file properly before starting to read from them otherwise, it will be blank and it will drive you crazy – Fredrik Jun 17 '20 at 21:01
  • Just FYI, make this can help. At the end of my post I have the final result that I wish to have. the f_list(output5.txt) is the fist if-statement code check is True. it pits to list together for matches. The da_List is (output6.txt - 2nd if-statement check bottom) and it also pits two list and checks for matches – Boomer Jun 17 '20 at 21:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/216168/discussion-between-fredrik-and-boomer). – Fredrik Jun 17 '20 at 21:09
  • Sorry Fredrik, I guess I missed the chat opportunity. Maybe we can touch base later? Thanks for your help. – Boomer Jun 17 '20 at 21:24
  • Fredrik, I was able to get your script to work by using it in a separate .py file. It's not an ideal setup but it works. I'll keep trying to combine the two files using the one script. Thanks for your help. – Boomer Jun 17 '20 at 21:53