ret = [] f = file("/tmp/basket.atf") for l in f.readlines(): if l.startswith("&P"): ret.append(l[1:8]+"\n") print l[1:8] print ret f = file("/tmp/basket.pnums","w") f.writelines(ret)