擅长:python、mysql、java
<p>我想你不太喜欢在那个文件中切片头,所以你必须先跳过它,不管怎样:</p>
<pre><code>with open('queryfile.txt') as input2:
# Consume the header
next(input2) # or input2.readline()
</code></pre>
<p>然后,函数必须将文件逐行读取为字符串,并在执行时去掉换行符:</p>
<pre><code>dna = ''.join(row.strip() for row in query_seq)
</code></pre>
<p>我还建议您稍微改变一下将切片索引传递给函数的方式:</p>
<pre><code>def slice_seq(query_seq, *slices):
</code></pre>
<p>这样,函数就可以接受0个或多个片作为位置参数,片在这里是索引的元组。综合起来你会得到:</p>
<pre><code>def slice_seq(query_seq, *slices):
# feel free to use rstrip, if strip seems redundant
dna = ''.join(row.strip() for row in query_seq)
return [dna[a:b + 1] for a, b in slices]
a=146
b=917
c=2381
d=2737
e=1
f=155
g=917
h=1057
with open('queryfile.txt') as input2:
next(input2)
answer = slice_seq(input2, (a, b), (c, d), (e, f), (g, h))
print(*answer, sep='\n')
</code></pre>