Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to access the Nth line of csv file

I have to access the Nth line in a CSV file.

Here's what I did:

import csv  the_file = open('path', 'r') reader = csv.reader(the_file)  N = input('What line do you need? > ') i = 0  for row in reader:     if i == N:         print("This is the line.")         print(row)         break      i += 1  the_file.close() 

...but this does not feel optimal. Edit for precision: If the file is huge, I do not want to go through all the lines and I do not want to have to load the whole file into memory.

I do hope something like reader[N] exists, but I have not found it.

Edit for answer: This line (coming from chosen answer) is what I was looking for:

next(itertools.islice(csv.reader(f), N, None) 
like image 432
Gabriel L'Heureux Avatar asked Dec 05 '14 01:12

Gabriel L'Heureux


People also ask

How do I open a CSV file without first row?

Line 1: We import the Pandas library as a pd. Line 2: We read the csv file using the pandas read_csv module, and in that, we mentioned the skiprows=[0], which means skip the first line while reading the csv file data. Line 4: Now, we print the final dataframe result shown in the above output without the header row.


1 Answers

You can use enumerate to iterate through the list until you find the right row:

for i, row in enumerate(reader):     if i == line_number:         print("This is the line.")         print(row)         break 

You can also use itertools.islice which is designed for this type of scenario - accessing a particular slice of an iterable without reading the whole thing into memory. It should be a bit more efficient than looping through the unwanted rows.

def get_csv_line(path, line_number):     with open(path) as f:         return next(itertools.islice(csv.reader(f), line_number, None)) 

But if your CSV file is small, just read the entire thing into a list, which you can then access with an index in the normal way. This also has the advantage that you can access several different rows in random order without having to reset the csv reader.

with open(path) as f:     my_csv_data = list(csv.reader(f)) print(my_csv_data[line_number]) 
like image 127
Stuart Avatar answered Oct 06 '22 01:10

Stuart