Wolfram ResearchProductsPurchasingServices & ResourcesAbout UsOur Sites
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.
Previous section-----Next section

2.12.7 Importing and Exporting Files

Import["file", "List"] import a one-dimensional list of data from a file
Export["file", list, "List"] export list to a file as a one-dimensional list of data
Import["file", "Table"] import a two-dimensional table of data from a file
Export["file", list, "Table"] export list to a file as a two-dimensional table of data
Import["file", "CSV"] import data in comma-separated format
Export["file", list, "CSV"] export data in comma-separated format

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

Out[1]=

Here are the contents of the file.

In[2]:=  !!out1.dat

Out[2]=

This imports the contents back into Mathematica.

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

Out[3]=

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 in Section 2.12.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"]

Out[4]=

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

In[5]:=  !!out2.dat


This imports the array back into Mathematica.

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

Out[6]=

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

Out[7]=

Here is the exported data.

In[8]:=  !!out3.dat


This imports the data back into Mathematica.

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

Out[9]=

With InputForm, you can explicitly see the strings.

In[10]:=  InputForm[%]

Out[10]//InputForm=

Import["file", "List"] treat each line as a separate numerical or other data item
Import["file", "Table"] treat each element on each line as a separate numerical or other data item
Import["file", "String"] treat the whole file as a single character string
Import["file", "Text"] treat the whole file as a single string of text
Import["file", "Lines"] treat each line as a string of text
Import["file", "Words"] treat each separated word as a string of text

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

Out[11]=

Here are the contents of the file.

In[12]:=  !!out4.dat


This imports the whole file as a single string.

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

Out[13]//InputForm=

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

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

Out[14]//InputForm=

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

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

Out[15]//InputForm=


Any questions about topics on this page? Click here to get an individual response.Buy NowFree TrialMore Information



 © 2008 Wolfram Research, Inc.  Terms of Use  Privacy Policy |
Sign up for our newsletter: