Importing and Exporting Files

Import["file","List"]import a onedimensional list of data from a file
Export["file",list,"List"]export list to a file as a onedimensional list of data
Import["file","Table"]import a twodimensional table of data from a file
Export["file",list,"Table"]export list to a file as a twodimensional table of data
Import["file","CSV"]import data in commaseparated format
Export["file",list,"CSV"]export data in commaseparated format

Importing and exporting lists and tables of data.

This exports a list of data to the file .
In[1]:=
Click for copyable input
Out[1]=
Here are the contents of the file.
This imports the contents back into the Wolfram Language.
In[3]:=
Click for copyable input
Out[3]=

If you want to use data purely within the Wolfram Language, then the best way to keep it in a file is usually as a complete Wolfram Language expression, with all its structure preserved, as discussed in "Reading and Writing Wolfram Language Files: Files and Streams". 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 twodimensional array of data.
In[4]:=
Click for copyable input
Out[4]=
When necessary, numbers are written in C or Fortranlike "E" notation.
This imports the array back into the Wolfram Language.
In[6]:=
Click for copyable input
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 Wolfram Language strings.

This exports a mixture of textual and numerical data.
In[7]:=
Click for copyable input
Out[7]=
Here is the exported data.
This imports the data back into the Wolfram Language.
In[9]:=
Click for copyable input
Out[9]=
With InputForm, you can explicitly see the strings.
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",{"Text","Lines"}]treat each line as a string of text
Import["file",{"Text","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]:=
Click for copyable input
Out[11]=
Here are the contents of the file.
This imports the whole file as a single string.
This imports the file as a list of lines of text.
This imports the file as a list of words separated by white space.