This page is still largely in progress.
Contents |
Not everybody use Mathematica. In this page, we discuss the problem of importing other people's data or exporting your own.
If the import/export remains within Mathematica, I find that, whatever you're dealing with, Put[] and Get[] are the most convenient tools to transit via the filesystem:
Put[something, "somewhere.dat"] itwas = Get["somewhere.dat"]
That can be slow and take up lot of space. You can also use DumpSave (format .mx) or Export as .wdx, which is more portable cross-machines and platforms. These are however a bit fragile solutions as there is no guarantee you can use this data elsewhere or later. Note that
Export["mydata.wdx", mydata]
that you can simply Import[] is preferable to DumpSave, that hard-codes variable names within the file, i.e.,
mynewdata = Import["mydata.wdx"] (* works *) otherdata = Import["mydata.mx"] (* that was DumpSaved, does not work *)
For the latter case, you must instead only Import["mydata.mx"] and that will restore the variable mydata.
Mathematica deals with many different types of data (from images and sounds to more abstract objects like lattice data or colors).
We consider cases as needs arise.
Mathematica deals with lists.
The most important operation is importing/exporting in columns for the outside world.
Export["out.dat", {{x1, 1}, {x2, 2}, {x3, 3}}]
will generate the file "out.dat" containing:
x1 1 x2 2 x3 3
Note that if x1, x2 and x3 are not defined, they will be exported as text.
The other way around, is achieved with the option "Table":
Import["out.dat", "Table"]
213.123.189.237,OK,GB,United Kingdom,Derbyshire,Chesterfield,,53.25,-1.4167 89.53.1.11,OK,DE,Germany,Niedersachsen,Jesteburg,,53.3,9.9667 192.167.73.58,OK,IT,Italy,Lombardia,Pavia,,45.1667,9.1667 66.249.65.116,OK,US,United States,California,Mountain View,94043,37.4192,-122.057 130.158.206.150,OK,JP,Japan,Ibaraki,Tsukuba,,36.0833,140.117
(first entry is an IP address, second is a status message, third country code, fourth name of the country, fifth the county, sixth the city, seventh the postcode, eight and nine the longitude and latitude).
This is best imported as "Data":
listIp=Import["ip-locations.txt", "Data"]
listIp[[1]] is the first line, listIp[[1, 5]] returns Derbyshire.
To export a to a file in a single time, Export[] is good enough:
Export["file.txt", "My data here, on various lines (for example).", "Text", CharacterEncoding -> "UTF8"]
CharacterEncoding could also be "ASCII", or "Text" could be "Table".
In older versions of Mathematica (prior to 6.0, apparently), you could pipe the result and thus append to an existing file by using something like
Export["!cat >>> file.txt", "More text\non various lines", "Text"]
However it appears this doesn't work anymore.
To append textual data to a file, I find the most convenient solution is to open a stream:
file = "file.txt"; str = OpenWrite[file]; WriteString[str, "One line\nor two..."] WriteString[str, "\n\nwell, just whatever you fancy...\n"] Close[str];