python - How to group text files based on content? -


i have 2 text files following content:

text file 1:-

imports kernel32.dll     0x40d1f0 loadlibrarya     0x40d1f4 getprocaddress     0x40d1f8 exitprocess advapi32.dll     0x40d200 regopenkeya exports 

text file2:-

imports kernel32.dll     0x419128 loadlibrarya     0x41912c getprocaddress     0x419130 exitprocess advapi32.dll     0x419138 regclosekey oleaut32.dll     0x419140 sysfreestring exports 

i want text file containing common outputs such grouped based on .dll names. e.g; desired output file:

 kernel32.dll         0x40d1f0 loadlibrarya         0x40d1f4 getprocaddress         0x40d1f8 exitprocess  advapi32.dll         0x40d200 regopenkeya         0x419138 regclosekey  oleaut32.dll         0x419140 sysfreestring 

i have written script read filename , keep appending output textfile final.txt. function of python can use group values based on headings?

#start import sys value = sys.argv[1] print value                 # value filename open(value) inputd: # parse file     line in inputd:         if line.strip() == 'imports':               break     line in inputd:          if line.strip() == 'exports':             break     if "none" not in line:             print line.rstrip()  # print line             open("final.txt", "a") outputd:                 outputd.write(line) # write output file #end  

my current output follows:

 kernel32.dll         0x40d1f0 loadlibrarya         0x40d1f4 getprocaddress         0x40d1f8 exitprocess  advapi32.dll         0x40d200 regopenkeya  kernel32.dll         0x419128 loadlibrarya         0x41912c getprocaddress         0x419130 exitprocess  advapi32.dll         0x419138 regclosekey  oleaut32.dll         0x419140 sysfreestring 

not perfect way it, implemented workaround, should :)

import sys value = sys.argv[1] print value       existing_dlls  = [] open("final.txt", "r") outputd:         #if file not exists make(an empty file) else file not find error thrown     line in outputd:         if line.lower().find(".dll")> -1:             existing_dlls.append(line.strip().lower())  dontwrite = false # flag keep track of address written  open("final.txt", "a") outputd:         #dont open file every write, rather keep open write operations     open(value) inputd: # parse file         line in inputd:                         #dont iterate again , again similar checks, if statements mutually exclusive             if line.strip() == 'imports':                   continue                        elif line.strip() == 'exports':                 continue             elif "none" not in line:                 if line.strip().lower().endswith(".dll"):                     if line.strip().lower() in existing_dlls:                         dontwrite = true                     else :                          dontwrite = false                 if not dontwrite:                                 outputd.write(line) 

Comments