outputing a list to a file mathematica
ret = {};
For[i = 1, i <= Length@x, i++,
AppendTo[ret, {idNum = x[[i, 1]] , a = x[[i, 2]], b = x[[i, 3]],
开发者_如何学编程c = x[[i, 4]], d = x[[i, 5]], e = x[[i, 6]], f = x[[i, 7]],
g = DateDifference[{d, e, f}, {currYear, currMonth, currDay}],
If[g > 90, Y, N]}];];
Print@ret
How do i output a list in to text file which has no bracket and braces, also need new line after each line.
You need to use the command Export
, eg to save an nxn array as comma separated values use something like:
data = RandomInteger[{0, 256}, {50, 50}]
Export[NotebookDirectory[] <> "data.csv", data, "CSV"]
The built-in formats are given in $ExportFormats
I like to use Export["ret.m", ret, "Lines"]
to get one entry per line in the output file ret.m
. YMMV depending on the structure of the stuff you're exporting.
Just as a sidetalk:
The usual way to program in Mathematica is functional, rather than procedural.
Variables are defined only when you need to inspect something, or preserve results for future work. Also, loops are discouraged.
An equivalent form of your program (just a quick draft), in these lines may be something like:
x = {{1, a1, b1, c1, 2010, 11, 12},
{1, a2, b2, c2, 2011, 12, 13}};
Export["c:\data.csv", #, "CSV"] &
[Flatten[
{#[[1 ;; 7]],
{#, If[# > 90, "Y", "N"]} &@
DateDifference[#[[5 ;; 7]], DateList[][[1 ;; 3]]]}
] & /@ x]
精彩评论