File processing

Chris Barker chrishbarker at home.net
Mon Jul 9 20:21:11 EDT 2001


Chris McMillan wrote:
> Why am I doing this you may ask: I have tens of data files that I need to
> strip the first line before I can process it in Matlab.  So, I dream of a
> script that opens every file in a directory, deletes the first line, .....

Not that I want to trow a wet blanket on your Python enthusiasm, but you
have gotten all the help you need with Python, no I'll try this:

If you want the read the file into MATLAB, why not use MATLAB? I am a
big Python fan, and do almost everything with it, including much that I
used to use MATLAB for, but if I needed to get data into MATLAB, I'd use
MATLAB. I suspect you are trying to use the load function to load a
table of data, but MATLAB chokes on the header line. The solution is to
use some of MATLAB's other file reading capabilities. Here is an example
function:


********* WARNING MATLAB code on the Python newsgroup *********

% a function that reads a file without the first line

function data = readfile(filename,cols,rows);

if nargin < 3;
  rows = inf;
end

if nargin < 2;
  cols = 1;
end


file = fopen(filename,'r');
% discard the first line
junk = fgetl(file);
data = fscanf(file,'%g',[cols,rows])';

return

*** END MATLAB CODE ****

I made a number of observations when I wrote this:

1) I wanted to use default argument values
2) I forgot all the semicolons
3) I forgot the "end"s
4) I missed Pytons object oriented syntax for file operations
5) I really like MATLAB's fscanf: I wish there was a Python option like
that.
6) I also miss Inf in Python

In Python, this function would look something like:

# a function that reads a file without the first line

def readfile(filename,rows = -1):

    file = open(filename,'r')
    # discard the first line
    junk = file.readline()
    data = []
    line = file.readline().strip()
    while line:
        if (rows < 0) or (len(data) < rows):
            data.append(map(float, line.strip().split()))
        line = file.readline().strip()
    return data

-Chris



-- 
Christopher Barker,
Ph.D.                                                           
ChrisHBarker at home.net                 ---           ---           ---
http://members.home.net/barkerlohmann ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------



More information about the Python-list mailing list