(Create the page)
 
Line 34: Line 34:
 
Import["out.dat", "Table"]
 
Import["out.dat", "Table"]
 
</pre>
 
</pre>
 +
 +
= Textual data =
 +
 +
Strings of text with one line encoding, separated by commas, e.g.,
 +
 +
<pre>
 +
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
 +
</pre>
 +
 +
(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":
 +
 +
<pre>
 +
listIp=Import["ip-locations.txt", "Data"]
 +
</pre>
 +
 +
<nowiki>listIp[[1]]</nowiki> is the first line, <nowiki>listIp[[1, 5]]</nowiki> returns Derbyshire.

Revision as of 17:13, 29 November 2009

This page is still largely in progress.

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.

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

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.