Re: Writing CSV files? [message #37522] |
Tue, 06 January 2004 14:12  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Jonathan Greenberg writes:
> Is there an easy way to write an array to an ASCII comma deliminated file
> (CSV)? I see a READ_ASCII command but no WRITE_ASCII. Thanks!
Here is an article that discusses one way to do this:
http://www.dfanning.com/tips/csv_file.html
You will find a reference there to a program named
WRITE_CSV_DATA that will write a 2D array and an
optional vector of column headers to a CSV file.
You might also think about WRITE_SYLK if you are trying
to write a file that can be opened in a spreadsheet.
Cheers,
David
--
David W. Fanning, Ph.D.
Fanning Software Consulting, Inc.
Phone: 970-221-0438, E-mail: david@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|
|
|
Re: Writing CSV files? [message #37606 is a reply to message #37522] |
Thu, 08 January 2004 01:46  |
Timm Weitkamp
Messages: 66 Registered: August 2002
|
Member |
|
|
Format codes provide a convenient way of bringing numbers in IDL into
shape for output to screen or files (see also "Format Codes" in the online
help). Why not use them in a WRITE_CSV routine, like this simple one:
PRO write_csv, fileName, data
nColumns = (SIZE(data))[1]
formatStr = '(' + STRTRIM(nColumns,2) + '(F, :,", "))'
OPENW, u, fileName, /GET_LUN, WIDTH=1000
PRINTF, u, data, FORMAT=formatStr
FREE_LUN, u
END
If the data arrays you want to write are very large, then you may want to
replace the single "PRINTF, ..." line above by a FOR loop to avoid memory
problems:
[...]
OPENW, u, fileName, /GET_LUN, WIDTH=1000
nRows = (SIZE(data))[2]
FOR iRow = 0, nRows-1 DO BEGIN
thisRow = STRING(data[*,iRow], FORMAT=formatStr)
PRINTF, u, thisRow
ENDFOR
FREE_LUN, u
[...]
Hope this helps,
Timm
--
Timm Weitkamp <http://people.web.psi.ch/weitkamp>
On 06.01.04 at 15:12 -0700, David Fanning wrote:
> Jonathan Greenberg writes:
>
>> Is there an easy way to write an array to an ASCII comma deliminated file
>> (CSV)? I see a READ_ASCII command but no WRITE_ASCII. Thanks!
>
> Here is an article that discusses one way to do this:
>
> http://www.dfanning.com/tips/csv_file.html
>
> [...]
|
|
|