Downloads

 

From Quantitative Finance

Jump to: navigation, search

Libraries

Utilities

backwards_text_file.py: Reading a text file backwards

[ Download backwards_text_file.py. ]

Reading a text file backwards is a relatively common task. Let me explain first what I mean by backwards: you read the file line by line, starting from the last line and progressing towards the first.

Why would you need this? Imagine that you have a large CSV (comma separated value) with numerous records sorted in ascending order by date/time. You want to read the last N records. Using the standard text file input/output machinery you would probably end up reading the entire file, discarding all but the last N records. Extremely wasteful. Chances are you will have more than one such file.

I have written a Python module to help you: backwards_text_file.py. It is really a port of Uri Guttman's Perl module File::ReadBackwards, which is also attempting to behave like Greg Ward's standard Python module distutils.text_file, and should be adaptable.

This module is very easy to use:

    from backwards_text_file import BackwardsTextFile

    in_file = BackwardsTextFile("test.csv")
    
    result = in_file.readlines()    

    print result
    
    in_file.close()

This will read the entire file backwards. To read it line by line, as you will need to do in most circumstances, use the following:

    from backwards_text_file import BackwardsTextFile

    in_file = BackwardsTextFile("test.csv")

    line = in_file.readline()
    while not line is None:
        print line
        line = in_file.readline()

    in_file.close()

That's it. I'm quite keen on testing this module on various operating systems and in various environment, so I would appreciate your feedback. Without further ado,

[ Download backwards_text_file.py. ]

 
 
Google
 
Personal tools