在python中的两个模式之间打印行 [英] Print lines between two patterns in python

查看:73
本文介绍了在python中的两个模式之间打印行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下结构的文件:

I have a file with the following structure:

@scaffold456
ATGTCGTGTCAGTG
GTACGTGTGTGG
+
!!!!!@!!!!!!!!
!!!!!!!!!!!!
@scaffold342
ATGGTGTCGTGGTG
ACGTGGC
+
!>!>!!!!+!!!!!
!!!!!!!

我想要这样的输出:

>scaffold456
ATGTCGTGTCAGTG
GTACGTGTGTGG
>scaffold342
ATGGTGTCGTGGTG
ACGTGGC

我想用Python实现这一点,我从以下内容开始:

I want to achieve this in Python, I started with the following:

fastq_filename = "test_file"
fastq = open(fastq_filename) # fastq is the file object

for line in fastq:
    if line.startswith("@"):
        print line.replace("@", ">")

但由于我不知道,我无法继续: 1.特定图案匹配后如何打印线条? 2.如何指定我要跳过+之间的行,直到下一个@符号?

but I can't go on anymore as I don't know: 1. How to print lines after a certain pattern match? 2. How I should specify that I want to skip lines between + till the next @ sign?

这是Python中一个更复杂的主题,我不知道,任何帮助和解释都将非常有用,谢谢!

This is a more complex topic in Python which I don't know, any help and explanation would be great, thanks!

推荐答案

fastq_filename = "test_file"
fastq = open(fastq_filename) # fastq is the file object    

canPrintLines = False # Boolean state variable to keep track of whether we want to be printing lines or not
for line in fastq:
    if line.startswith("@"):
        canPrintLines = True # We have found an @ so we can start printing lines
        line = line.replace("@", ">")
    elif line.startswith("+"):
        canPrintLines = False # We have found a + so we don't want to print anymore

    if canPrintLines:
        print(line)

这篇关于在python中的两个模式之间打印行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆