How do i read a csv file from a specific line 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 csv file from a specific line 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.6k4 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 badges492 silver badges548 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.

Xem thêm:  Có bao nhiêu phát biểu sau đây đúng với sản phẩm chuyên môn hoá của Duyên hải Nam Trung Bộ

answered Oct 20, 2014 at 11:34

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

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 csv file from a specific line in python?

answered Oct 17, 2021 at
7:31

codeholic24codeholic24

8773 silver badges18 bronze badges

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

Step 1: Load the CSV file using the open method in a file object. Step 2: Create a reader object with the help of DictReader method using fileobject. This reader object is also known as an iterator can be used to fetch row-wise data. Step 3: Use for loop on reader object to get each row.

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

Pandas Read CSV.

Load the CSV into a DataFrame: import pandas as pd. df = pd.read_csv(‘data.csv’) … .

Print the DataFrame without the to_string() method: import pandas as pd. … .

Check the number of maximum returned rows: import pandas as pd. … .

Increase the maximum number of rows to display the entire DataFrame: import pandas as pd..

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 without the header in Python?

To read CSV file without header, use the header parameter and set it to “None” in the read_csv() method.

Thuộc website harveymomstudy.com

Related Posts