求助,用python保留文本文件中需要的字符,去掉多余的字符
我想将我的数据库中多余的信息去掉如:> sp|A8X858|DRE2_CAEBR Anamorsin homolog OS=Caenorhabditis briggsae GN=CBG09135 PE=3 SV=2
MSAALSDFLKQSNLLQVTDGGPLVAGTDGVEGTRVDLVGEVEHAIIQVQE
TELLAEVCNTVFDVMKQNGEVLVFSTDLTTAHRKLRIAGFRVTELAADWP
ARGVKSVNFGDKVTLDLGNATVEEDLIDEDGLLQEEDYEKPTGDQLKAGG
CGPDDPNKKKRACKNCNCGLAEQEEAEKMGKIAEASAAKSSCGNCALGDA
FRCATCPYLGQPPFKPGETVKLSSVDDF
> sp|Q90660|BIR_CHICK Inhibitor of apoptosis protein OS=Gallus gallus GN=ITA PE=2 SV=1
MNIMDSSPLLASVMKQNAHCGELKYDFSCELYRMSTFSTFPVNVPVSERR
LARAGFYYTGVQDKVKCFSCGLVLDNWQPGDNAMEKHKQVYPSCSFVQNM
LSLNNLGLSTHSAFSPLVASNLSPSLRSMTLSPSFEQVGYFSGSFSSFPR
DPVTTRAAEDLSHLRSKLQNPSMSTEEARLRTSHAWPLMCLWPAEVAKAG
LDDLGTADKVACVNCGVKLSNWEPKDNAMSEHRRHFPNCPFVENLMRDQP
SFNVSNVTMQTHEARVKTFINWPTRIPVQPEQLADAGFYYVGRNDDVKCF
CCDGGLRCWESGDDPWIEHAKWFPRCEYLLRVKGGEFVSQVQARFPHLLW
NSSCTTSDKPVDENMDPIIHFEPGESPSEDAIMMNTPVVKAALEMGFSRR
LIKQTVQSKILATEENYKTVNDLVSELLTAEDEKREEEKERQFEEVASDD
LSLIRKNRMALFQRLTSVLPILGSLLSAKVITELEHDVIKQTTQTPSQAR
ELIDTVLVKGNAAASIFRNCLKDFDPVLYKDLFVEKSMKYVPTEDVSGLP
MEEQLRRLQEERTCKVCMDKEVSIVFIPCGHLVVCKECAPSLRKCPICRG
TIKGTVRTFLS
想将>后面的多余信息去掉最终保留成如下的格式:
>A8X858
MSAALSDFLKQSNLLQVTDGGPLVAGTDGVEGTRVDLVGEVEHAIIQVQE
TELLAEVCNTVFDVMKQNGEVLVFSTDLTTAHRKLRIAGFRVTELAADWP
ARGVKSVNFGDKVTLDLGNATVEEDLIDEDGLLQEEDYEKPTGDQLKAGG
CGPDDPNKKKRACKNCNCGLAEQEEAEKMGKIAEASAAKSSCGNCALGDA
FRCATCPYLGQPPFKPGETVKLSSVDDF
>Q90660
MNIMDSSPLLASVMKQNAHCGELKYDFSCELYRMSTFSTFPVNVPVSERR
LARAGFYYTGVQDKVKCFSCGLVLDNWQPGDNAMEKHKQVYPSCSFVQNM
LSLNNLGLSTHSAFSPLVASNLSPSLRSMTLSPSFEQVGYFSGSFSSFPR
DPVTTRAAEDLSHLRSKLQNPSMSTEEARLRTSHAWPLMCLWPAEVAKAG
LDDLGTADKVACVNCGVKLSNWEPKDNAMSEHRRHFPNCPFVENLMRDQP
SFNVSNVTMQTHEARVKTFINWPTRIPVQPEQLADAGFYYVGRNDDVKCF
CCDGGLRCWESGDDPWIEHAKWFPRCEYLLRVKGGEFVSQVQARFPHLLW
NSSCTTSDKPVDENMDPIIHFEPGESPSEDAIMMNTPVVKAALEMGFSRR
LIKQTVQSKILATEENYKTVNDLVSELLTAEDEKREEEKERQFEEVASDD
LSLIRKNRMALFQRLTSVLPILGSLLSAKVITELEHDVIKQTTQTPSQAR
ELIDTVLVKGNAAASIFRNCLKDFDPVLYKDLFVEKSMKYVPTEDVSGLP
MEEQLRRLQEERTCKVCMDKEVSIVFIPCGHLVVCKECAPSLRKCPICRG
TIKGTVRTFLS
下面是我的程序 不知道为什么总是有错 希望能帮帮我 `·· 初学者请指教
程序代码:
import sys import re import os def SELECT (filename1): f1 = open (filename1) f2 = open ("111.txt","w") #存放结果的文件 line1 = f1.readline() while line1: temp1 = re.sub('^\s+','',line1) #去除开头的空白 temp1 = re.sub('\s+$','',temp1) #去除开头的空白 p1 = (r'>') p2 = (r'|') result = p1.match(temp1) if(result): columns1 = p2.match(temp1) columns2 = columns1.strip('||') print columns2 else print temp1 f1.close() f2.close() SELECT('anti2.txt')