Re: saving a list.
- To: mathgroup at smc.vnet.net
- Subject: [mg18957] Re: saving a list.
- From: "Atul Sharma" <atulksharma at yahoo.com>
- Date: Fri, 30 Jul 1999 01:33:47 -0400
- References: <7nlolp$bam@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
It's inelegant, but it works. You may want to change the default path for the save location in both writeOutput and readInput (I like things to appear on the desktop). Both work with standard tab delimited format, suitable for spreadsheets, text editors etc. I routinely write both string and numeric information, hence type "Word" is specified. You will want to convert your input to numerics using ToExpression[ ] or change the type to "Number" if this is all you require. One question though, If your data input and output only involves Mathematica, why not use the Put and Get commands on native list structures? i.e. arrayOut >>"c:\\windows\\desktop\\test.txt"; arrayIn = <<"c:\\windows\\desktop\\test.txt"; ____________________________________________________________________________ __ Clear[writeOutput, readInput] writeOutput[outputList_?ListQ,saveName_?StringQ, path_String:"C:\\Windows\\Desktop\\"]:=Module[{lineout,wstrm,outputLine}, wstrm=OpenAppend[path<>saveName<>".txt"]; Do[ lineout=""; outputLine=outputList[[j]]; Do[ lineout=lineout<>ToString[outputLine[[i]],FormatType->OutputForm]<>Tab, {i,1,Length[outputLine]}]; WriteString[wstrm,lineout<>"\n"], {j,1,Length[outputList]}]; Close[wstrm]; Print["File Output: ",path<>saveName<>".txt"]; ]; (*************************************************************************** *******) readInput[saveName__?StringQ,path_String:"C:\\Windows\\Desktop\\"]:= Module[{rstrm,results}, rstrm= OpenRead[path<>saveName]; results=ReadList[rstrm,Word,WordSeparators->{"\t",","}, RecordSeparators -> {"\n"},RecordLists->True] ; Close[rstrm]; results]; Dave Berube wrote in message <7nlolp$bam at smc.vnet.net>... >I am writing a program that inputs 2 lists of numbers each with two >columns, and creates a 3rd list also with two columns. > >For Example, lets say that the files file.txt and file2.txt look like this: > >file.txt file2.txt > >1 1.5 0 3.6 >2 4.6 2 1.9 >3 6.8 6 4.2 > >I can read them into Mathematica with ReadList > A = ReadList["file.txt", {Number, Number}] > B = ReadList["file2.txt", {Number, Number}] > >then I do all my operations that I need to do on them and come up with a >two column matrix C. > >In plain old Mathematica form it looks like, say, > >C = {{1, 2.7}, {2, 1.9}, {3, 3.0}} > >But I want to get a file, call it file3.txt, that contains just 2 columns, >like file.txt and file2.txt. > >I've tried TableForm and MatrixForm, etc. to format the output and then >used Save to save it, but when I open the text file, I get all this junk >in there (Gridbox, TableForm, and all the braces and stuff actually in the >text file). > >So is there a way for me to have Mathematica save the information as a >plain old text file with two columns of numbers, no words, no braces, none >of that junk. > >Thanks for any help. >-Dave Berube > >