擅长:python、mysql、java
<p>您可以在csv读取器对象上使用<code>deque</code>尾部配方。你知道吗</p>
<p>我知道你有头球,留着吧。然后使用此处复制的<a href="https://docs.python.org/2/library/collections.html#deque-recipes" rel="nofollow noreferrer">tail recipe</a>:</p>
<pre><code>def tail(filename, n=10):
'Return the last n lines of a file'
return deque(open(filename), n)
</code></pre>
<p>在interator上调用<code>deque</code>(如文件或csv读取器或任何其他形式的Python迭代器)等同于在同一对象上调用<code>list(iterator)</code>。但是使用<code>deque</code>可以限制大小并在该迭代器上创建Unix的<code>tail</code>实用程序的等价物。你知道吗</p>
<p>下面是一个使用大范围对象并仅保留最后5个对象的示例:</p>
<pre><code>>>> from collections import deque
>>> deque(range(100000),5)
deque([99995, 99996, 99997, 99998, 99999], maxlen=5)
</code></pre>