(Textual data)
Line 37: Line 37:
 
= Textual data =
 
= Textual data =
  
Strings of text with one line encoding, separated by commas, e.g.,
+
== Importing ==
 +
 
 +
* Strings of text with one line encoding, separated by commas, e.g.,
  
 
<pre>
 
<pre>
Line 56: Line 58:
  
 
<nowiki>listIp[[1]]</nowiki> is the first line, <nowiki>listIp[[1, 5]]</nowiki> returns Derbyshire.
 
<nowiki>listIp[[1]]</nowiki> is the first line, <nowiki>listIp[[1, 5]]</nowiki> returns Derbyshire.
 +
 +
== Exporting ==
 +
 +
To export a to a file in a single time, <nowiki>Export[]</nowiki> is good enough:
 +
 +
<pre>
 +
Export["file.txt", "My data here,
 +
on various lines (for example).", "Text", CharacterEncoding -> "UTF8"]
 +
</pre>
 +
 +
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
 +
 +
<pre>Export["!cat >>> file.txt", "More text\non various lines", "Text"]</pre>
 +
 +
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:
 +
 +
<pre>
 +
file = "file.txt";
 +
 +
str = OpenWrite[file];
 +
 +
WriteString[str, "One line\nor two..."]
 +
 +
WriteString[str, "\n\nwell, just
 +
whatever
 +
you fancy...\n"]
 +
 +
Close[str];
 +
</pre>

Revision as of 17:50, 29 November 2009

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.

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