This is documentation for Mathematica 4, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)
Wolfram Research, Inc.

Manipulating Files and DirectoriesReading Textual Data

2.11.7 Importing and Exporting Files

Importing and exporting lists and tables of data.

This exports a list of data to the file out1.dat.

In[1]:= Export["out1.dat", {6.7, 8.2, -5.3}, "List"]


Here are the contents of the file.

In[2]:= !!out1.dat




This imports the contents back into Mathematica.

In[3]:= Import["out1.dat", "List"]


If you want to use data purely within Mathematica, then the best way to keep it in a file is usually as a complete Mathematica expression, with all its structure preserved, as discussed on page 2.11.1. But if you want to exchange data with other programs, it is often more convenient to have the data in a simple list or table format.

This exports a two-dimensional array of data.

In[4]:= Export["out2.dat", {{5.6 10^12, 7.2 10^12}, {3, 5}}, "Table"]


When necessary, numbers are written in C or Fortran-like "E" notation.

In[5]:= !!out2.dat

"5.6e12 7.2e12

3 5"

This imports the array back into Mathematica.

In[6]:= Import["out2.dat", "Table"]


If you have a file in which each line consists of a single number, then you can use Import["file", "List"] to import the contents of the file as a list of numbers. If each line consists of a sequence of numbers separated by tabs or spaces, then Import["file", "Table"] will yield a list of lists of numbers. If the file contains items that are not numbers, then these are returned as Mathematica strings.

This exports a mixture of textual and numerical data.

In[7]:= Export["out3.dat", {{"first", 3.4}, {"second", 7.8}}]


Here is the exported data.

In[8]:= !!out3.dat

"first 3.4

second 7.8"

This imports the data back into Mathematica.

In[9]:= Import["out3.dat", "Table"]


With InputForm, you can explicitly see the strings.

In[10]:= InputForm[%]

Out[10]//InputForm= {{"first", 3.4}, {"second", 7.8}}

Importing files in different formats.

This creates a file with two lines of text.

In[11]:= Export["out4.dat",

{"The first line.", "The second line."}, "Lines"]


Here are the contents of the file.

In[12]:= !!out4.dat

"The first line.

The second line."

This imports the whole file as a single string.

In[13]:= Import["out4.dat", "Text"]//InputForm

Out[13]//InputForm= "The first line.\nThe second line.\n"

This imports the file as a list of lines of text.

In[14]:= Import["out4.dat", "Lines"]//InputForm

Out[14]//InputForm= {"The first line.", "The second line."}

This imports the file as a list of words separated by white space.

In[15]:= Import["out4.dat", "Words"]//InputForm

Out[15]//InputForm= {"The", "first", "line.", "The", "second", "line."}

Manipulating Files and DirectoriesReading Textual Data