printing only the last 8 entries in a .csv file

Posted by user2341103 on Stack Overflow See other posts from Stack Overflow or by user2341103
Published on 2013-06-27T03:31:12Z Indexed on 2013/06/27 4:21 UTC
Read the original article Hit count: 105

Filed under:
|

I have an input csv file as below,I want to print only the most recent 8 entries..can anyone provide inputs on how to do this?

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

© Stack Overflow or respective owner

Related posts about python

Related posts about csv