I have two files
File1:
IdName1 Info1 Info2 Info3 #Info: from program1 for name1 #Info: from program2 for name1
IdName2 Info1 Info2 Info3 #Info: from program1 for name2 #Info: from program2 for name2
IdName4 Info1 Info2 Info3 #Info: from program1 for name4
IdName3 Info1 Info2 Info3 #Info: from program1 for name3 #Info: from program2 for name3
File2:
# ProgramInfo
# Query: IdName1 Info1 Info2 Info3
# DatabaseInfo
# FiledInfo
line1
line2
# ProgramInfo
# Query: IdName2 Info1 Info2 Info3
# DatabaseInfo
# FiledInfo
# ProgramInfo
# Query: IdName4 Info1 Info2 Info3
# DatabaseInfo
# FiledInfo
line1
line2
line3
line4
Now I need to check if “IdName1 Info1 Info2 Info3” is in File2 after “#Query: ” and if it is I need to split up the iformation from that line in File1 and
insert it in File2 before the corresponding “# ProgramInfo” line. The out put file should look like this:
OutputFile:
# IdName1 Info1 Info2 Info3
# Info: from program1 for name1
# Info: from program2 for name1
# ProgramInfo
# Query: IdName1 Info1 Info2 Info3
# DatabaseInfo
# FiledInfo
line1
line2
# IdName2 Info1 Info2 Info3
# Info: from program1 for name2
# Info: from program2 for name2
# ProgramInfo
# Query: IdName2 Info1 Info2 Info3
# DatabaseInfo
# FiledInfo
# IdName4 Info1 Info2 Info3
# Info: from program1 for name4
# ProgramInfo
# Query: IdName4 Info1 Info2 Info3
# DatabaseInfo
# FiledInfo
line1
line2
line3
line4
My question is now, how do I add the corresponding three lines into File2, I have been trying something like this:
import sys
def programs_info_comb(fileName1, fileName2):
my_file1 = open(fileName1, "r")
my_line1=my_file1.readlines()
my_file2 = open(fileName2, "r")
my_line2=my_file2.readlines()
for line1 in my_line1:
(name1, info1, info2)= line1.strip().split("\t")
for line2 in my_line2:
if line2.startswith("# Q"):
name2 = line2[9:-1]
if name1 == name2:
#### here Im lost how to tell where I want those next two lines to be printed
print "#"+" "+name1
print info1
print info2
my_file1.close
my_file2.close
if __name__== "__main__":
programs_info_comb(sys.argv[1], sys.argv[2])
There is probably a better easier way to this, all help will be gratefully accepted
Thank you for your time
Daeja
I have loaded file1 into a dictionary for lookup later and sent the output to stdout. Before sending the output I’ve checked the type of line I’m on and output accordingly.
Here’s the o/p :-