擅长:python、mysql、java
<p>问题是,由于<code>site-20</code>是负数,Python认为它是相对于序列末尾的索引,因此切片是空的(因为第一个索引在最后一个索引之后)。一定不要低于0。你知道吗</p>
<pre><code>data=myfile.read()
str1 = data[site:site+1+20]
temp = data[max(site-20, 0):site]
final_sequence = temp+str1
print final_sequence
</code></pre>
<p>或更短</p>
<pre><code>data=myfile.read()
final_sequence = data[max(site-20, 0):site+1+20]
print final_sequence
</code></pre>
<p>注意,不需要使用<code>min(site+1+20, len(data))</code>作为上限,因为Python会自动将序列末尾以外的切片索引剪辑为序列长度。你知道吗</p>