我有多个文本文件,格式如下(见下文),我想提取> TAIR:175以及每个文件下面的字符串。然后我想将每个> TAIR:175 +字符放在一个新文件中:
前:
文件1:
>TAIR:175 a
ALSKDJFLKAHGLKASJDFLAKJSDLKGHALKSDHGALKALKSJDF
>TAIR:674 a
ASLALKSDGHLA;KSJDFIEURALKSDHGLANVAL;KSDJGHKLJA
>TAIR:812 a
KLJALSKDHGLAKSDHJFIEUROWASDLKGNIEASDFJKWERLJKJ
文件2:
>TAIR:975 b
KLJALSKDHGLAKSDHJFIEUROWASDLKGNIEASDFJKWERLJKJ
>TAIR:175 b
ZZZLAALSKDJFALKSDJFL;KJEIURALKDJFNVALKSDJFKZZZ
>TAIR:444 b
QQALKSDJFWOIAOQIWUERTOIUQTOIUOQIWEURLASKDJFA;KL
新文件:
>TAIR:175 a
ALSKDJFLKAHGLKASJDFLAKJSDLKGHALKSDHGALKALKSJDF
>TAIR:175 b
ZZZLAALSKDJFALKSDJFL;KJEIURALKDJFNVALKSDJFKZZZ
感谢您的帮助。
答案 0 :(得分:0)
with open('3.txt', 'w') as f_out: # open result file
for fn in ('1.txt', '2.txt'): # iterate through multiple files
with open(fn, 'r') as f_in:
for line in f_in:
if line.startswith('>TAIR:175 '):
f_out.write(line)
f_out.write(f_in.readline())