0

I have a python script which takes input from a admin.csv file. I want to read the latest entry in the admin.csv file, which is on the last line. Kindly help me out. My csv file is below.

Input_IP    Skip_Frames Save_Video  Id_Display
192.168.1.97    100             True    False
191.168.43.97   91              False   True
191.168.43.97   1               True    True
191.168.4.61    11              False   False
191.135.4.12    99              True    True
Aakash
  • 47
  • 1
  • 1
  • 10
  • Possible duplicate of [Find the last row from a CSV input Python](https://stackoverflow.com/questions/53483389/find-the-last-row-from-a-csv-input-python) – ytu May 04 '19 at 05:52
  • @ytu Yes sir, thank you. It helped me get an explanation. – Aakash May 04 '19 at 06:04

2 Answers2

1

One simple way if you are using pandas is

import pandas as pd

df = pd.read_csv('filename.csv')
df.iloc[-1]
chink
  • 1,505
  • 3
  • 28
  • 70
  • glad that helped. U can accept or upvote if the answer solves the problem – chink May 04 '19 at 12:12
  • dear sir, I already accepted the solution provided by Miklos, and since I am a new user on this website so it wont show my up votes here, I did up voted your answer though. I am sorry about it, hope you understand. – Aakash May 05 '19 at 02:23
0

The csv reader is a generator, you can convert that to a list. You can get the last data in the last line with the following code sample:

import csv

with open('test.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=' ')
    print(list(csv_reader)[-1][-1])