社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

kantal

kantal 最近创建的主题
kantal 最近回复了
6 年前
回复了 kantal 创建的主题 » 基于python中两个短序列的过滤行

如果文件不太大,可以立即读取,并使用re.findall():

    import re
    with open("infile.txt") as finp:
        data=finp.read()
    with open('outfile.txt', "w") as f:
        for item in re.findall(r">.+?[\r\n\f][AGTC]*?AATAAA[AGTC]{2,}GGAC[AGTC]*", data):
            f.write(item+"\n")

"""
+? and *?       means non-greedy process;
>.+?[\r\n\f]    matches a line starting with '>' and followed by any characters to the end of the line; 
[AGTC]*?AATAAA  matches any number of A,G,T,C characters, followed by the AATAAA pattern; 
[AGTC]{2,}      matches at least two or more characters of A,G,T,C;
GGAC            matches the GGAC pattern;
[AGTC]*         matches the empty string or any number of A,G,T,C characters.
"""