Re: WriteList function?
- To: mathgroup at smc.vnet.net
- Subject: [mg9346] Re: [mg9320] WriteList function?
- From: seanross at worldnet.att.net
- Date: Sat, 1 Nov 1997 03:33:32 -0500
- Sender: owner-wri-mathgroup at wolfram.com
Tom Thomas wrote: > > Hi, I am a new Mathematica user. > > I have had success reading files of space-separated numbers using > ReadList, but now need to output my results in the same fashion. i.e. > one row of the matrix should appear on one text line, > no commas, nor curly braces are desired in the output. > > I have searched the manual for a corresponding WriteList function > without success. > > Any suggestions? I may have to resort to awk/sed/perl to postprocess > the file, but I would've thought Mathematica would've provided the > facilities to write data in a general format. > > Thank you kindly, --tom thomas > ------------------------------------------------------------------- > example: > In[1]:= !! /tmp/mymat > 1 2 3 > 4 5 6 > 7 8 9 > > In[1]:= mydata = ReadList["/tmp/mymat", Number, RecordLists->True] > > Out[1]= {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} > > In[2]:= stmp = OpenWrite["/tmp/myoutput"] > > Out[2]= OutputStream[/tmp/myoutput, 4] > > In[3]:= Write[stmp, mydata] > > %cat /tmp/myoutput > {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} > > I need to see instead: > 1 2 3 > 4 5 6 > 7 8 9 Don't despair! There is no built in command to do this, but you can use the built-in string commands and make up a string that suits you then write that to a file. The way I typically do it is: StringReplace[ ToString[myarray], {","->" ", "{{"->" ", "{"->" ", "}}"->"\n", "}"->"\n"}] This makes the array into a string, then replaces all the commas and left curly braces with spaces and the right curly braces with return characters. You can then Write the result to a file.