Loop over a file and write the next line if a condition is met

Posted by 111078384259264152964 on Stack Overflow See other posts from Stack Overflow or by 111078384259264152964
Published on 2010-03-25T08:02:14Z Indexed on 2010/03/25 8:03 UTC
Read the original article Hit count: 282

Having a hard time fixing this or finding any good hints about it. I'm trying to loop over one file, modify each line slightly, and then loop over a different file. If the line in the second file starts with the line from the first then the following line in the second file should be written to a third file.

!/usr/bin/env python

with open('ids.txt', 'rU') as f: with open('seqres.txt', 'rU') as g: for id in f: id=id.lower()[0:4]+'_'+id[4] with open(id + '.fasta', 'w') as h: for line in g: if line.startswith('>'+ id): h.write(g.next())

All the correct files appear, but they are empty. Yes, I am sure the if has true cases. :-) "seqres.txt" has lines with an ID number in a certain format, each followed by a line with data. The "ids.txt" has lines with the ID numbers of interest in a different format. I want each line of data with an interesting ID number in its own file.

Thanks a million to anyone with a little advice!

© Stack Overflow or respective owner

Related posts about python

Related posts about string-manipulation