Re: Export, Import, Convert Types Again?

*To*: mathgroup at smc.vnet.net*Subject*: [mg57245] Re: Export, Import, Convert Types Again?*From*: Bill Rowe <readnewsciv at earthlink.net>*Date*: Sat, 21 May 2005 02:41:22 -0400 (EDT)*Sender*: owner-wri-mathgroup at wolfram.com

On 5/20/05 at 4:43 AM, leenewm at umich.edu (Lee Newman) wrote: >Q: What is the (best) way to save a big expression of data to be >used later in the same notebook without having to do type >conversion processing? >Situation: >1) I have a notebook that imports data from various Excel >spreadsheets and converts the data to the desired types. When >complete, I "save" the expression containing the data using Export, >i.e, Export["featureDB.tsv", "TSV"]. I do this so I don't have to >re-process the data everytime I do analysis on it in this notebook. >2) When I open the noteobook again, I import the data using the >statement: featureDB = Import["featureDB.tsv"] >Problem: Many of type conversions that I took the time to process >are lost. Using Head /@ featureDB[[1]] before and after the >export/re-import, the types are as follows: >Before: {Integer, Integer, List, List, List, Integer, List, List, >Symbol, List} After : {Integer, Integer, String, String, String, >Integer, String, String, String, String} >So it seems that lists become Strings as Symbols (e.g. True, >False, Null). I use Export so I can view the data outside >Mathematica using a text editor -- should I use Save instead? Is >there another Export format that will preserve mathematica >expressions like lists upon Import? A combination of Import/Export is quite useful when data is in a format not native to Mathematica or when you want the data saved in a format easier to read in a different application. But once you have imported the data into Mathematica and plan only to use the data in Mathematica using either Put or DumpSave is much more efficient. Put[expr, "filename"] or equivalently, expr >> "filename", writes all of the values associated with expr to a text file in a format Mathematica understands. Then in a future notebook you can restore things using Get as in expr = Get["filename"] or equivalently expr = << "filename". An alternative which is more efficient, would be DumpSave. DumpSave["file.nx", symbol] writes everything associated with symbol including the name to a binary file. Then Get["file.mx"] or <<"file.mx" restores things including symbol names. In fact you can save the entire Global` context and restore it using DumpSave and Get. The disadvantage of DumpSave is the binary format is not portable accross platforms. That is if I used DumpSave to create a file on my Mac and send it to someone using Windows, they will not be able to read the file correctly. For myself, I typically use Export{"filename", expr, "CSV"] and modify the file with a text editor so that I can restor things using expr = << "filename". This is somewhat more effort than simply using Put. I prefer my method over Put becuase the file Put creates is not neatly formated and is consequently more difficult to read in something other than Mathematica. -- To reply via email subtract one hundred and four