Page 1 of 1

Separate CSV columns into files

Posted: Tue Mar 31, 2009 7:17 pm
by t-rex22
I have files of comma separated data with columns: time, velocity, acceleration, and displacement (for example). I want to write files that combine time with each of these columns (i.e., time and velocity, time and acceleration, etc...).

I cannot figure out how to call each column and then write only the columns I want to a file. Preferably, all files (some files have more than four columns, but the first column is always "time") would be written within the initial file selection mouseUp.

It seems like it should be easy, but I just haven't got there yet. Any help/direction would be greatly appreciated!!

Thanks!

:cry:

Posted: Tue Mar 31, 2009 7:53 pm
by bn
Welcome t-rex22,
try this

Code: Select all

on mouseUp
    -- answer file "please open csv file" 
    -- if it is empty then exit mouseUp
    -- put it into theFile
    -- put url ("file:" & theFile) into tAllCSV 
    -- -- you can uncomment abov code or
    --  -- load your data into a variable, tAllSCV is used in this example
    put empty into tTimeVelo
    put empty into tTimeAccel
    put empty into tTimeDisplace
    repeat for each line aLine in tAllCSV
        put item 1 of aLine & comma & item 2 of aLine & return after tTimeVelo
        put item 1 of aLine & comma & item 3 of aLine & return after tTimeAccel
        put item 1 of aLine & comma & item 4 of aLine & return after tTimeDisplace
    end repeat
    -- delete trailing return
    delete last char of tTimeVelo
    delete last char of tTimeAccel
    delete last char of tTimeDisplace
    -- now write the tTimexxx variables to a 3 files
end mouseUp
it gives you the variables with again comma separated values.
regards
Bernd

Posted: Wed Apr 01, 2009 4:59 pm
by t-rex22
Thanks Bernd, that was very helpful!!