How do i read a specific line in a csv file in python?

I have a CSV file with 100 rows.

How do I read specific rows?

I want to read say the 9th line or the 23rd line etc?

Burhan Khalid

164k18 gold badges238 silver badges276 bronze badges

asked Oct 20, 2014 at 11:27

How do i read a specific line in a csv file in python?

1

You could use a list comprehension to filter the file like so:

with open('file.csv') as fd:
    reader=csv.reader(fd)
    interestingrows=[row for idx, row in enumerate(reader) if idx in (28,62)]
# now interestingrows contains the 28th and the 62th row after the header

answered Oct 20, 2014 at 11:44

ch3kach3ka

11.4k4 gold badges29 silver badges26 bronze badges

3

Use list to grab all the rows at once as a list. Then access your target rows by their index/offset in the list. For example:

#!/usr/bin/env python

import csv

with open('source.csv') as csv_file:
    csv_reader = csv.reader(csv_file)
    rows = list(csv_reader)

    print(rows[8])
    print(rows[22])

answered Feb 22, 2018 at 21:26

Alan W. SmithAlan W. Smith

23.7k4 gold badges66 silver badges92 bronze badges

You simply skip the necessary number of rows:

with open("test.csv", "rb") as infile:
    r = csv.reader(infile)
    for i in range(8): # count from 0 to 7
        next(r)     # and discard the rows
    row = next(r)   # "row" contains row number 9 now

Jürgen K.

3,2959 gold badges28 silver badges62 bronze badges

answered Oct 20, 2014 at 11:29

Tim PietzckerTim Pietzcker

318k56 gold badges493 silver badges549 bronze badges

You could read all of them and then use normal lists to find them.

with open('bigfile.csv','rb') as longishfile:
    reader=csv.reader(longishfile)
    rows=[r for r in reader]
print row[9]
print row[88]

If you have a massive file, this can kill your memory but if the file's got less than 10,000 lines you shouldn't run into any big slowdowns.

answered Oct 20, 2014 at 11:34

How do i read a specific line in a csv file in python?

0

You can do something like this :

with open('raw_data.csv') as csvfile:
    readCSV = list(csv.reader(csvfile, delimiter=','))
    row_you_want = readCSV[index_of_row_you_want]

answered Apr 25, 2018 at 18:01

xtigerxtiger

1,4351 gold badge15 silver badges30 bronze badges

1

May be this could help you , using pandas you can easily do it with loc

'''
Reading 3rd record using pandas -> (loc)
Note : Index start from 0 
If want to read second record then 3-1 -> 2
loc[2]` -> read second row and `:` -> entire row details 
'''

import pandas as pd
df = pd.read_csv('employee_details.csv')
df.loc[[2],:]

Output :

How do i read a specific line in a csv file in python?

answered Oct 17, 2021 at 7:31

codeholic24codeholic24

8773 silver badges18 bronze badges

How do I read a specific row in a CSV file in Python?

Using reader.
Step 1: In order to read rows in Python, First, we need to load the CSV file in one object. So to load the csv file into an object use open() method..
Step 2: Create a reader object by passing the above-created file object to the reader function..
Step 3: Use for loop on reader object to get each row..

How do I read one column from a CSV file in Python?

File Used: file. Here, we have the read_csv() function which helps to read the CSV file by simply creating its object..
Import the module..
Read data from CSV file..
Convert it into the list..
Print the list..

How do I read specific rows in pandas?

Steps to Select Rows from Pandas DataFrame.
Step 1: Data Setup..
Step 2: Import CSV Data..
Step 3: Select Rows from Pandas DataFrame..
Select pandas rows using the iloc property..
Select pandas rows using loc property..

How do I read a CSV file row by row in Python using pandas?

15 ways to read CSV file with pandas.
Example 1 : Read CSV file with header row..
Example 2 : Read CSV file with header in second row..
Example 3 : Skip rows but keep header..
Example 4 : Read CSV file without header row..
Example 5 : Specify missing values..
Example 6 : Set Index Column..
Example 7 : Read CSV File from External URL..