簡體   English   中英

檢查文件中的Python字符串並從文件中寫入新文件

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

我正在嘗試幫助我的搭檔學習他的Python課,但是我對Python並不了解。 這是他被困了幾個小時的問題,我們將不勝感激!

問題:程序將接受DNA序列的文本文件(文件中每行1個序列),並在滿足以下條件時確定該序列是否有效:1.長度是3的倍數。2.以ATG開頭。 3.以TAG結尾。 然后程序將DNA序列后跟“ True”或“ False”寫入新文件(每個輸出在單獨的行中)。 示例:輸入序列:輸入文件中的'ATGCGCCTGCGTCTGTACTAG'。 輸出:'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