擅长:python、mysql、java
<p>一种方法是先知道<code>csv2</code>中的行数,然后使用<code>itertools.islice</code>切掉最后一行。你知道吗</p>
<pre><code>>>> from itertools import islice
>>> import csv
with open('csv2.csv') as f:
line_count = sum(1 for _ in f) #get the line count
f.seek(0) #move the file pointer to the start of the file
f = islice(f, 0, line_count-1) #skip last line
reader = csv.reader(f)
print list(reader)
...
[['abc4', 'def4', 'ghi4'], ['abc5', 'def5', 'ghi5']]
</code></pre>