We are currently building PyThalesians, a fully featured Python based library for analysing financial markets. Whilst most of it will remain proprietary, we are planning to open up the product to the public by open sourcing some of its content at our GitHub site in the future here. More information about PyThalesians can be found here here.
backwards_text_file.py: Reading a text file backwards
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:
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:
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,
- This page was last modified on 29 June 2015, at 08:59.
- This page has been accessed 34,159 times.