我有一个如下的输入 csv 文件,我只想打印最近的 8 个条目。任何人都可以提供有关如何执行此操作的输入吗?
INPUT:-
trend.csv
['2013-06-25 20:01', '10']
['2013-06-25 20:06', '9']
['2013-06-25 20:06', '8']
['2013-06-26 20:06', '7']
['2013-06-26 20:06', '6']
['2013-06-26 20:06', '5']
['2013-06-26 20:06', '4']
['2013-06-26 20:06', '3']
['2013-06-26 20:06', '2']
['2013-06-26 20:08', '1']
OUTPUT:-
['2013-06-25 20:06', '8']
['2013-06-26 20:06', '7']
['2013-06-26 20:06', '6']
['2013-06-26 20:06', '5']
['2013-06-26 20:06', '4']
['2013-06-26 20:06', '3']
['2013-06-26 20:06', '2']
['2013-06-26 20:08', '1']
代码:
import csv
#Now read the recent 8 entries and print
cr = csv.reader(open("trend.csv","rb"))
for row in cr:
#print only the recent most 8 entries
print row