在Python的基因组.txt文件中搜索特定实例

时间:2018-11-30 23:34:54

标签: python python-3.x

我目前正在尝试搜索包含基因组的.txt文件以查找'G'和'C'的特殊实例,我可以通过将代码复制并粘贴到程序中来使该程序正常工作,但是我无法弄清楚从文件中获取它。我猜测问题是代码跨越多行,但不知道如何解决。

这是我一直在使用的代码,将代码复制到python时可以使用:

sequence='**strings of code**'
found=0

size(400, 100)
background(0)
fill(255)

for letter in sequence:
    if letter=='C' or letter=='G':
        background(0)
        found+=1
        text('The GC content of the sequence is: %', 50, 50)
        text((float(found)/(len(sequence)/100.0)),250,50)

这是我现在无法使用的代码:

f = open('sequence.txt', 'r').read()
sequence=f.split('\n')

found=0


size(400, 100)
background(0)
fill(255)


for letter in sequence:
    if letter=='C' or letter=='G':
    background(0)
    found+=1
    text('The GC content of the sequence is: %', 50, 50)
    text((float(found)/(len(sequence)/100.0)),250,50)

1 个答案:

答案 0 :(得分:0)

简短回答:

删除此行

sequence=f.split('\n')

并使用:

sequence = open('sequence.txt', 'r').read()

长答案:

我将假定第二个代码段中的缩进不是这里的问题,真正的问题是它不能为您提供正确的答案。

我可以在您使用的第一个片段中看到

sequence='**strings of code**'

这使序列的值成为字符串,以便在对其进行迭代时,字母变量将包含单个字符,但是,当您尝试从文件中读取它时,使用了对结果字符串的分割方法,该结果字符串将序列从字符串转换为字符串列表,因此这里的字母变成一个完整的字符串,代表了'\ n'之前的每一行,而不是像您在第一个代码段中那样的单个字母

因此,删除该行将使序列像以前一样成为要迭代的字符串

相关问题