Lee Newman wrote:
> Hello,
>
> Situation:
> - I am reading data in from a stream (the file is too big to import)
> and processing it chunk by chunk.
> - The proesssed data looks something like data = {{100, 1.5, -10},
> {200,3.2, 10},...etc}
> - I have opened an output stream to write the data using s =
> OpenWrite[file, FormatType -> OutPutForm];
> - I want to write the data in tab or comma separated format with one row
> per line.
>
> Problem:
> - No matter what I've tried, I always get either
> (i) output with rows enclosed in brackets, or
> (ii) delimited text output, but with Mathematica automatically adding a line
> break if a line exceeds what seems to be the default width for output format
> - I've tried using Write and WriteString -- nothing seems to work.
> - I also tried generating an exact string for each line using a function
> that inserts tabs between list items and adds a newline at the end of
> the list.
>
As Stephan Linnik pointed out (msg 57980), you can pass an OutputStream
to Export and take advantage of its formatting capabilities.
As far as team (ii) from above, you have to set the option PageWidth
when writing to an OutputStream to Infinity if you don't want the line
wrapping.
> ExpressionToTSV[list_] := Module[{temp},
> temp = ToString@Rest@(ToString /@ Flatten@({"\t", #} & /@ list));
> StringReplace[temp, {"{" -> "", "}" -> "", ", " -> ""}];
> StringInsert[temp, "\n", -1]
> ];
>
Not as easy as using Export, but if for some reason you really wanted to
use WriteString, then you could do this:
In[110]:= data={{123,2.123,123},{123,2.123,123},{123,2.123,123}};
In[112]:= strm=OpenWrite["c:\\test.txt"];
In[113]:= (WriteString[strm, StringReplace[ToString[Flatten[#1]],
{"{" -> "", "}" -> "", ", " -> ","}], "\n"] & ) /@ data;
In[114]:= Close[strm];
In[115]:= !!"c:\\test.txt"
From In[115]:=
123,2.123,123
123,2.123,123
123,2.123,123
--
Igor Antonio
Wolfram Research, Inc.
http://www.wolfram.com
To email me personally, remove the dash.