Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8049321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:45:29+00:00 2026-06-05T06:45:29+00:00

I know how to do it for a TXT file, but now I am

  • 0

I know how to do it for a TXT file, but now I am having some trouble doing it for a CSV file.

How can I read a CSV file from the bottom in Python?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:45:30+00:00Added an answer on June 5, 2026 at 6:45 am

    Pretty much the same way as for a text file: read the whole thing into a list and then go backwards:

    import csv
    with open('test.csv', 'r') as textfile:
        for row in reversed(list(csv.reader(textfile))):
            print ', '.join(row)
    

    If you want to get fancy, you could write a lot of code that reads blocks starting at the end of the file and working backwards, emitting a line at a time, and then feed that to csv.reader, but that will only work with a file that can be seeked, i.e. disk files but not standard input.


    Some of us have files that do not fit into memory, could anyone come with a solution that does not require storing the entire file in memory?

    That’s a bit trickier. Luckily, all csv.reader expects is an iterator-like object that returns a string (line) per call to next(). So we grab the technique Darius Bacon presented in “Most efficient way to search the last x lines of a file in python” to read the lines of a file backwards, without having to pull in the whole file:

    import os
    
    def reversed_lines(file):
        "Generate the lines of file in reverse order."
        part = ''
        for block in reversed_blocks(file):
            for c in reversed(block):
                if c == '\n' and part:
                    yield part[::-1]
                    part = ''
                part += c
        if part: yield part[::-1]
    
    def reversed_blocks(file, blocksize=4096):
        "Generate blocks of file's contents in reverse order."
        file.seek(0, os.SEEK_END)
        here = file.tell()
        while 0 < here:
            delta = min(blocksize, here)
            here -= delta
            file.seek(here, os.SEEK_SET)
            yield file.read(delta)
    

    and feed reversed_lines into the code to reverse the lines before they get to csv.reader, removing the need for reversed and list:

    import csv
    with open('test.csv', 'r') as textfile:
        for row in csv.reader(reversed_lines(textfile)):
            print ', '.join(row)
    

    There is a more Pythonic solution possible, which doesn’t require a character-by-character reversal of the block in memory (hint: just get a list of indices where there are line ends in the block, reverse it, and use it to slice the block), and uses chain out of itertools to glue the line clusters from successive blocks together, but that’s left as an exercise for the reader.


    It’s worth noting that the reversed_lines() idiom above only works if the columns in the CSV file don’t contain newlines.

    Aargh! There’s always something. Luckily, it’s not too bad to fix this:

    def reversed_lines(file):
        "Generate the lines of file in reverse order."
        part = ''
        quoting = False
        for block in reversed_blocks(file):
            for c in reversed(block):
                if c == '"':
                    quoting = not quoting
                elif c == '\n' and part and not quoting:
                    yield part[::-1]
                    part = ''
                part += c
        if part: yield part[::-1]
    

    Of course, you’ll need to change the quote character if your CSV dialect doesn’t use ".

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I read floats from a .txt file. Depending on the name at
Task 1: Read each row from one csv file into one seprate txt file.
I am creating a file abc.txt and storing the data. How can I know
I am running into a project that read a stream from a txt file,
I've got a fstream my_file(test.txt), but I don't know if test.txt exists. In case
I just want to know: How can I delete 'log.txt' if the last modification
Cited from http://xss-proxy.sourceforge.net/Advanced_XSS_Control.txt : As many here probably know, current XSS attacks typically come
I know you can not set a key value dynamically, but what about the
I've been searching for a while now, but php-noob as I am, can't get
I know this is a noob question, but I've worked with Python before and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.