简体   繁体   English

检查文件中的Python字符串并从文件中写入新文件

[英]Python String manipulation from a file and writing into a new file after checking the string

I'm trying to help my partner with his Python class, but I don't know much about Python. 我正在尝试帮助我的搭档学习他的Python课,但是我对Python并不了解。 Here's the problem he has been stuck for hours, any help would be appreciated! 这是他被困了几个小时的问题,我们将不胜感激!

Question: The program will accept a text file of DNA sequences (1 sequence per line in the file) and determine if the sequence is valid when the following criteria is met: 1. length is a multiple of 3. 2. starts with ATG. 问题:程序将接受DNA序列的文本文件(文件中每行1个序列),并在满足以下条件时确定该序列是否有效:1.长度是3的倍数。2.以ATG开头。 3. ends with TAG. 3.以TAG结尾。 The program will then write the DNA sequence followed by 'True' or 'False' to a new file (each output on a separate line). 然后程序将DNA序列后跟“ True”或“ False”写入新文件(每个输出在单独的行中)。 Example: Input sequence: 'ATGCGCCTGCGTCTGTACTAG' from input file. 示例:输入序列:输入文件中的'ATGCGCCTGCGTCTGTACTAG'。 Output: 'ATGCGCCTGCGTCTGTACTAG True' to the output file. 输出:'ATGCGCCTGCGTCTGTACTAG True'到输出文件。

def check_sequence(sequence):
    if len(sequence) % 3:
        return False
    if sequence[:3] != 'ATG':
        return False
    if sequence[-3:] != 'TAG':
        return False


def process_file(input_file_path, output_file_path):
    with open(input_file_path) as input_file:
        with open(output_file_path, 'w') as output_file:
            for map(str.rstrip, input_file):
                output_file.write(line)
                output_file.write(' ')
                output_file.write(str(check_sequence(line)))
                output_file.write('\n')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM