我使用下面的代碼合併兩個文本文件的行爲不內循環:的Python:與預期
def combine_acpd_ccs(self, ccs_file, acps_file, out_file):
with open(ccs_file, 'r') as in_file1:
with open(acps_file, 'r') as in_file2:
with open(out_file, 'w') as out1:
out1.write('PDB\tPA\tEHSS\tACPS\n')
for line in in_file1:
segs = line.split()
for i in in_file2:
sse_score = i.split()
#print line
#print segs
if segs[0][:-4] == sse_score[0]:
out1.write(segs[0][:-4]+'\t'+segs[1]+'\t'+segs[2]+'\t'+sse_score[1]+'\n')
實例數據的模樣:
ccs_file:
1b0o.pdb 1399.0 1772.0
1b8e.pdb 1397.0 1764.0
acps_file:
1b0o 0.000756946316066
1b8e 8.40662008775
1b0o 6.25931529116
我希望我出去把要像:
PDB PA EHSS ACPS
1b0o 1399.0 1772.0 0.000756946316066
1b0o 1399.0 1772.0 6.25931529116
1b8e 1397.0 1764.0 8.40662008775
但我的代碼只是產生預期我輸出的最上面兩行。如果我在第二個for
循環中打印segs
,則只會將ccs_file
中的第一行傳遞給循環。任何想法,我出了什麼問題?
您可以將所有三個'with'語句放在一行上,以減少一些醜陋的嵌套。 – wim