我有两个文本文件,我在|
之前匹配了txt1的第二列和txt2的第一列,我只是在下面提到它,如果它们相同的话,我会输出,还有它的描述,一切都很好,但是我没有得到输出中的new line discription
,它只是显示了它的第一行。你知道吗
以下是txt1.txt的内容
E5 E7 Bat
以下是txt2.txt的内容:
?E7|E5
Addsadsadsadsadsadsdasd
Sdsdfsdfdsfdsfdfdsfdsfd
AasadsaddccxcvcsAAGCAGT
这是我正在使用的代码
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
我认为您需要一些状态处理-“在匹配块中”/“不在匹配块中”。我重写了你算法的第二部分:
相关问题 更多 >
编程相关推荐