Reputation: 553
I have 2 text files , from which I match the second column of txt1 with first column of txt2 before |
, well I just mention it below , than I output if they are same , along with its discription , everything is going fine , but I didn't get the new line discription
in the output , it just showed its first line.
Here is the content for txt1.txt
E5 E7 Bat
Here is the content for txt2.txt:
?E7|E5
Addsadsadsadsadsadsdasd
Sdsdfsdfdsfdsfdfdsfdsfd
AasadsaddccxcvcsAAGCAGT
This is the code which i am using
with open('txt1.txt', 'rb') as file1:
#file1_data = dict(line.split()[1] for line in file1 if line.strip())
file1_data = [line.split()[1] for line in file1 if line.strip()]
#print file1_data
with open('txt2.txt', 'rb') as file2, open('output.txt', 'wb') as outputfile:
output = ""
file2lines = file2.readlines()
for i in range(len(file2lines)):
line = file2lines[i]
if line[0] == '?':
row = line.strip().split('|')
key = row[0][1:]
if key in file1_data:
output += line + "\t" + file2lines[i+1]
outputfile.write(output)
outputfile.close()
Getting output
?E7|E5
Addsadsadsadsadsadsdasd
Required output
?E7|E5
Addsadsadsadsadsadsdasd
Sdsdfsdfdsfdsfdfdsfdsfd
AasadsaddccxcvcsAAGCAGT
Upvotes: 0
Views: 98
Reputation: 16371
I think you need some state processing - "in matching block" / "not in matching block". I have rewritten second part of your algorithm:
with open('txt2.txt', 'rb') as file2, open('output.txt', 'wb') as outputfile:
output = []
do_block = False
for line in file2:
line = line.rstrip()
if not line: continue
if line[0] == '?':
key = line.strip().split('|')[0][1:]
do_block = key in file1_data
if do_block:
output.append(line)
outputfile.write("\n".join(output))
Upvotes: 1