(Textual data)
(From within Mathematica)
Line 4: Line 4:
  
 
Not everybody use [[Mathematica]]. In this page, we discuss the problem of importing other people's data or exporting your own.
 
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 (in some particular cases DumpSave[] is preferable):
 +
 +
<pre>
 +
Put[something, "somewhere.dat"]
 +
itwas = Get["somewhere.dat"]
 +
</pre>
  
 
Mathematica deals with many different types of data (from images and sounds to more abstract objects like lattice data or colors).
 
Mathematica deals with many different types of data (from images and sounds to more abstract objects like lattice data or colors).

Revision as of 11:45, 16 May 2010

This page is still largely in progress.

Contents

Importing and exporting data with Mathematica

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 (in some particular cases DumpSave[] is preferable):

Put[something, "somewhere.dat"]
itwas = Get["somewhere.dat"]

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.

Numerical data

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"]

Textual data

Importing

  • Strings of text with one line encoding, separated by commas, e.g.,
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.

Exporting

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];