Python:打印到文件直到空行?

时间:2014-11-19 13:17:29

标签: python if-statement file-io printing

我目前有:

for i in range (srange, erange):
    lines=open("%s\%s\propkaoutputfiles\propka_protein_output_%s%s.pka" %(filepath, name, aminoacid, i), "r")
    FILE=open("%s\%s\propkamanip\%sinpropka%s.txt" %(filepath, name, aminoacid, i), "w")
    for line in lines:
        if "0.00    0" and " B   7."in line and (line.split()[0]) == "%s" %aminoacid:
            print >> FILE, " %s in %s Structure %s" %(aminoacid, name, i)
            print >> FILE, line
        if "0.00    0" and " B   8."in line and (line.split()[0]) == "%s" %aminoacid:
            print >> FILE, " %s in %s Structure %s" %(aminoacid, name, i)
            print >> FILE, line

我正在阅读的数据看起来像这样,所以我想在我的“if in line”中嵌入一个代码,如果它符合该条件,将采用该行并将文档中的下一行打印到我的新文件中,直到找到一个空行:

LYS B 8.X 0.00 0

LYS B

LYS B

这里空行

ARG B 8.9 0.00

Python newb在这里,谢谢!

1 个答案:

答案 0 :(得分:0)

file1 = open("your input file", "r")
file2 = open("your output file", "w")

while True :
    line = file1.readline()
    if line :
        if "your criteria ..... " in line :
            file2.write("your header here\n")
            file2.write(line)
            line = file1.readline()
            while line and line != "\n" :
                file2.write(line)
                line = file1.readline()
    else :
        file1.close()
        file2.close()
        break