Extracting first 2 rows and last row from .txt or .csv Python

python read last n lines of file
python read last line of csv file
read last line of file python
extract data from csv file python
iterate over csv rows python
python csv read specific row
python read csv
python read csv line by line

I need to extract the first 2 rows and last row from a lot of .txt and .csv files. How can I allow a user to choose a file and it output a new .txt or .csv file with just those 3 rows in?

This is what you need:

def extract_lines(filename,outputname):
    l = []
    with open(filename,'r') as f: 
        for index,line in enumerate(f): #This iterates the file line by line which is memory efficient in case the csv is huge.
            if index < 2: #first 2 lines
        if index > 1: # means the file has at least 3 lines
    with open(outputname,'w') as f:
        for line in l:

Python: Get last N lines of a text file, like tail command – thispointer , I need to extract the first 2 rows and last row from a lot of .txt and .csv files. How can I allow a user to choose a file and it output a new .txt or .csv file with just those  Example #2: Extracting multiple rows with index. In this example, multiple rows are extracted first by passing a list and then by passing integers to extract rows between that range. After that, both the values are compared.

def get_lines(filename, front=2, rear=1):
    result = []
    with open(filename, 'rb') as f:
        for i, val in enumerate(f):
            if i >= front:

        back_pos = -2
        f.seek(back_pos, 2)  # jump to the second end byte

        rear_count = 0
        while True:
            if '\n' in f.read(1):
                rear_count += 1

            if rear_count >= rear:

            back_pos -= 1
            f.seek(back_pos, 2)

    return result

It's easy to read first row, but hard to read last row. To iter rows is very slowly.

How to grab the last row of a CSV file using Python, We have created a function to read last N lines from a text file, As file is read completely, if there is still data in buffer, then its first line. This function accepts 2 arguments i.e. a file path as a string and an integer N ( number of lines to be read from last). Read the last line of a text file or CSV file or log file. Python | Pandas Extracting rows using .loc[] Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.

I think you can also use the bash script to achieve this requirement.


for file in $(find . -name '*.txt' -o -name '*.csv' )
    sed -n -e '1,2p' -e '$p' ${file} > "result"${file:(-5)}

This script will search for all files ending in txt or csv. And it will cut the first two lines and the last line, store these lines in a new file.

For example, I have three files named file1.txt, file2.txt, file3.csv, it will cut the three lines for each file, and store them in result1.txt, result2.txt, result3.csv respectively.

Extract, Transform, and Save CSV data • fredgibbs.net, print (df.head()) #it will give you first five rows from your csv file How do I save an Excel file with large numbers as text in a csv file (details in my answer)?. 2,692 Views · How does one extract arrays from CSV files with columns in Python? So some_list[-1] gets the last element, some_list[-2] gets the second to last, etc. Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

In that way you can return the lines that you want is only question to play with the range


def etiqueta(df):
    for line,x in zip(df,range(0,2)):
    return lista


Get the last line from each of a large number of files, transform them , The first thing to do is to open my original CSV and read it. This is not useful in itself, so let's loop through all the lines in that file and print Fortunately, Python makes it very easy to read and write CSV files that can do a lot of hard work for us​. outputfile = open('placelist.txt','w') for row in inputfile: place = row[2].replace('  First of all, let’s import numpy module i.e. [11 22 33] [43 77 89]] Rows from Index 1 to last row : [[11 22 33] [43 77 89]] *** Select Columns by Index from a

Python for Beginners: Reading & Manipulating CSV Files, A simple way to read the last line of a file uses ReadList to get all lines from the file and The filename portion of the path is extracted using FileNameTake and then our 5" TI_2000_0.13_846.csv,"x[1] -> 8,x[2] -> 7,x[3] -> 7,x[4] -> 7,x[5] -> 7,​x[6] -> 7,x[7] -> 0 The first question is whether this overhead is really a problem. I know this is quite an old question - A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Let's say, you want to extract the first 2 rows and first 3 columns

How to read and write a CSV files with Python, Opening a CSV file; Looping through rows; Extracting information from a CSV file Converting lists to sets; Eventually, outputting the differences between the 2 sets Python allows you to open text files such as these and read their content, This will become our program's first line: import csv. We're now ready to write the  Using -TotalCount parameter is recommended. Especially, if you have a big CSV file. By this way, you will only read the first 10 lines of your CSV file instead of reading all lines, selecting the first 10 lines and extracting them. Get-Content C:\Temp\Test.csv -TotalCount 3 | Out-File C:\Temp\Test10lines.csv This works in Powershell 2 onwards.

Text::CSV, by Scott Davidson (Last modified: 05 Dec 2018) Note that this CSV has a header titles on the first row: The csv module in Python can be used to quickly parse CSV files into different #prompt the user for a file to import filter = "CSV file (*.csv)|*.csv|*.txt|All Files (*.*)|*. Lines 2 thru 8 are standard lines from the Rhino. 2. Using Python Array Slice Syntax. The standard python array slice syntax x[apos:bpos:incr] can be used to extract a range of rows from a DataFrame. However, the pandas documentation recommends the use of more efficient row access methods presented below. 2.1. First Few Rows. You can also use the head() method for this operation.

  • @Adirio Nope. It is meant to be outside of the loop. the If statement checks the no of lines in the file is at least 3 lines. if the file has only 2 lines, then there is no point to add the "last" line as the first 2 lines include the "last".
  • True, my bad. I would actually improve the efficiency by reading the 2 first lines out of the loop and looping with an instant discard. This way the if would not need to be evaluated for every line, which can take some time for big files.
  • @Adirio, True. If the question mentions quickest way, I would probably use seek instead.