【问题标题】:Python String manipulation from a file and writing into a new file after checking the string从文件中操作 Python 字符串并在检查字符串后写入新文件
【发布时间】:2015-10-21 07:09:06
【问题描述】:

我正在尝试帮助我的伙伴学习 Python 课程,但我对 Python 了解不多。这是他被困了几个小时的问题,任何帮助将不胜感激!

问题: 该程序将接受 DNA 序列的文本文件(文件中每行 1 个序列),并在满足以下条件时确定该序列是否有效: 1.长度是3的倍数。 2. 从 ATG 开始。 3. 以 TAG 结尾。 然后程序会将后跟“True”或“False”的 DNA 序列写入一个新文件(每个输出在单独的行上)。 例子: 输入序列:来自输入文件的“ATGCGCCTGCGTCTGTACTAG”。 输出:‘ATGCGCCTGCGTCTGTACTAG True’到输出文件。

【问题讨论】:

    标签: python


    【解决方案1】:
    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')
    

    【讨论】:

    • 我想你也需要.rstrip这些行。
    猜你喜欢
    • 1970-01-01
    • 2011-07-12
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    • 2019-01-24
    相关资源
    最近更新 更多