MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Write/WriteString -- writing delimited txt to a stream?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg58013] Re: [mg57944] Write/WriteString -- writing delimited txt to a stream?
  • From: Igor Antonio <igora at wolf-ram.com>
  • Date: Thu, 16 Jun 2005 05:36:41 -0400 (EDT)
  • References: <200506130951.FAA29393@smc.vnet.net>
  • Reply-to: igora at wolf-ram.com
  • Sender: owner-wri-mathgroup at wolfram.com

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.


  • Prev by Date: Re: For Loop and Array related
  • Next by Date: Re: For Loop and Array related
  • Previous by thread: Write/WriteString -- writing delimited txt to a stream?
  • Next by thread: Re: Write/WriteString -- writing delimited txt to a stream?