CSV (.csv)

MIME type: text/comma-separated-values, text/csv
CSV tabular data format.
Stores records of numerical and textual information as lines, using commas to separate fields.
Commonly used in spreadsheet applications as an exchange format.
CSV is an acronym for Comma-Separated Values.
Plain text format.
Similar to TSV.
  • Import and Export fully support the CSV format and provide various data conversion and formatting options.
  • Import automatically recognizes common number formats, including C and Fortran notations.
  • Numbers without decimal points are imported as integers.

Import and ExportImport and Export

  • Import["file.csv"] imports a CSV file, returning an array.
  • Export["file.csv",expr] exports a table to a CSV file.
  • Import["file.csv"] returns a list of lists containing strings and numbers, representing the rows and columns stored in the file.
  • Export["file.csv",expr] creates a CSV file from a table expr, using the CForm of non-numeric table elements.
  • The Wolfram Language automatically transforms arbitrary expressions to a list of lists when exporting to CSV.
  • Import["file.csv",elem] imports the specified element from a CSV file.
  • Import["file.csv",{elem,suba,subb,}] imports a subelement.
  • Import["file.csv",{{elem1,elem2,}}] imports multiple elements.
  • The import format can be specified with Import["file","CSV"] or Import["file",{"CSV",elem,}].
  • Export["file.csv",expr, elem] creates a CSV file by treating expr as specifying element elem.
  • Export["file.csv",{expr1,expr2,},{{elem1,elem2,}}] treats each as specifying the corresponding .
  • Export["file.csv",expr,opt1->val1,] exports expr with the specified option elements taken to have the specified values.
  • Export["file.csv",{elem1->expr1,elem2->expr2,},"Rules"] uses rules to specify the elements to be exported.
  • See the reference pages for full general information on Import and Export.
  • ImportString and ExportString support the CSV format.

Notebook InterfaceNotebook Interface

  • Save Selection As exports the selected part of a notebook as a CSV file.

ElementsElements

  • General Import elements:
  • "Elements"list of elements and options available in this file
    "Rules"full list of rules for each element and option
    "Options"list of rules for options, properties and settings
  • Data representation elements:
  • "Data"two-dimensional array, representing the rows and columns of a .csv file
    "Grid"table data as a Grid object
  • Import and Export use the element by default.

OptionsOptions

  • General Export options:
  • AlignmentNonehow data is aligned within table columns
    CharacterEncoding"UTF8"raw character encoding used in the file
  • Possible settings for Alignment are None, Left, Center, and Right.
  • General Import option:
  • CharacterEncoding"ASCII"raw character encoding used in the file
  • Import and Export options:
  • "EmptyField"""how to represent empty fields
    "TextDelimiters"Automaticstring or list of strings used to delimit non-numeric fields
  • By default, Export delimits text fields containing commas with double-quote characters.
  • With an explicit setting of , Export always uses the specified string tokens to delimit text fields, effectively cycling through the list of delimiters.
  • puts double quotes around each text field when exporting to CSV.
  • Double-quote characters delimiting text fields are not imported by default.
  • Import options:
  • "CurrencyTokens"{{"$", "£", "¥", "€"}, {"c", "¢", "p", "F"}}currency units to be skipped when importing numerical values
    "DateStringFormat"Nonedate format, given as a DateString specification
    "EmptyField"""how to represent empty fields
    "HeaderLines"0number of lines to skip at the beginning of the file
    "IgnoreEmptyLines"Falsewhether to ignore empty lines
    "Numeric"Truewhether to import data fields as numbers if possible
    "NumberSigns"{"-","+"}strings to use for signs of negative and positive numbers
  • Import converts table entries formatted as specified by the option to a DateList representation of the form .
  • With "Numeric" -> False, numbers will be imported as strings in the form they appear in the file.
  • Import automatically recognizes all common conventions for the encoding of line-separator characters.
  • Export option:
  • "TableHeadings"Noneheadings for table columns and rows
  • can be set to the following values:
  • Noneno labels
    Automaticgives successive integer labels for columns and rows
    {"col1","col2",}list of column labels
    {rhead,chead}specifies separate labels for the rows and columns
  • Export encodes line separator characters using the convention of the computer system on which the Wolfram Language is being run.

ExamplesExamplesopen allclose all

Basic Examples  (2)Basic Examples  (2)

Import a CSV file, showing the first 10 entries:

In[1]:=
Click for copyable input
Out[1]//TableForm=

Import this file, specifying what format to use for parsing dates:

In[2]:=
Click for copyable input
Out[2]=

Read all data from the file and plot it using DateListPlot:

In[3]:=
Click for copyable input
Out[3]=

The Wolfram Language automatically converts expressions to a list of lists when exporting to CSV:

In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=
Introduced in 1999
(4.0)
| Updated in 2012
(9.0)