没有得到输出中的全文

时间:2013-05-12 19:42:47

标签: python

我有两个文本文件,我将第二列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

1 个答案:

答案 0 :(得分:1)

我认为你需要一些状态处理 - "匹配块" /"不在匹配块"。我重写了算法的第二部分:

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))