python怎么提取特定字符所在的行,并写入一个新的文件里面
文件内容:DEFINITIONSchistosomajaponicumSJCHGC07869proteinmRNA,partialcds.ACCESSIONAY81083...
文件内容:
DEFINITION Schistosoma japonicum SJCHGC07869 protein mRNA, partial cds.
ACCESSION AY810830
VERSION AY810830.1 GI:60600350
KEYWORDS HTC.
SOURCE Schistosoma japonicum
提取VERSION和SOURCE这两行的字符串,然后把这两行字符串写入一个.txt文件里面 展开
DEFINITION Schistosoma japonicum SJCHGC07869 protein mRNA, partial cds.
ACCESSION AY810830
VERSION AY810830.1 GI:60600350
KEYWORDS HTC.
SOURCE Schistosoma japonicum
提取VERSION和SOURCE这两行的字符串,然后把这两行字符串写入一个.txt文件里面 展开
1个回答
2018-04-02
展开全部
# coding=utf-8
str1 = """
DEFINITION Schistosoma japonicum SJCHGC07869 protein mRNA, partial cds.
ACCESSION AY810830
VERSION AY810830.1 GI:60600350
KEYWORDS HTC.
SOURCE Schistosoma japonicum
"""
str1 = str1.split("\n")
with open("out.txt", "w") as f:
f.write(str1[3] + "\n")
f.write(str1[5] + "\n")
追问
不是,我要的是通过关键字符来提取行。不是通过行数来提取,因为文件很大,后面还有很多行,总不能一直靠输入行数来提取吧
追答
# coding=utf-8
import re
str1 = """
DEFINITION Schistosoma japonicum SJCHGC07869 protein mRNA, partial cds.
ACCESSION AY810830
VERSION AY810830.1 GI:60600350
KEYWORDS HTC.
SOURCE Schistosoma japonicum
DEFINITION Schistosoma japonicum SJCHGC07869 protein mRNA, partial cds.
ACCESSION AY810830
VERSION AY810830.1 GI:60600350
KEYWORDS HTC.
SOURCE Schistosoma japonicum
DEFINITION Schistosoma japonicum SJCHGC07869 protein mRNA, partial cds.
ACCESSION AY810830
VERSION AY810830.1 GI:60600350
KEYWORDS HTC.
SOURCE Schistosoma japonicum
DEFINITION Schistosoma japonicum SJCHGC07869 protein mRNA, partial cds.
ACCESSION AY810830
VERSION AY810830.1 GI:60600350
KEYWORDS HTC.
SOURCE Schistosoma japonicum
DEFINITION Schistosoma japonicum SJCHGC07869 protein mRNA, partial cds.
ACCESSION AY810830
VERSION AY810830.1 GI:60600350
KEYWORDS HTC.
SOURCE Schistosoma japonicum
DEFINITION Schistosoma japonicum SJCHGC07869 protein mRNA, partial cds.
ACCESSION AY810830
VERSION AY810830.1 GI:60600350
KEYWORDS HTC.
SOURCE Schistosoma japonicum
"""
re1 = r"VERSION.*"
re2 = r"SOURCE.*"
str11 = re.findall(re1, str1)
str22 = re.findall(re2, str1)
with open("out.txt", "w") as f:
if len(str11) > len(str22):
for i in range(0, len(str11)):
f.write(str11[i] + "\n")
f.write(str22[i] + "\n")
else:
for i in range(0, len(str22)):
f.write(str11[i] + "\n")
f.write(str22[i] + "\n")
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询