CSV (.csv)

Background

    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

  • Import["file.csv"] returns a list of lists containing strings and numbers, representing the rows and columns stored in the file.
  • Import["file.csv",elem] imports the specified element from a CSV file.
  • Import["file.csv",{elem,sub1,}] imports subelements, specifically useful for partial data import.
  • The import format can be specified with Import["file","CSV"] or Import["file",{"CSV",elem,}].
  • Export["file.csv",expr] creates a CSV file from expr, using the InputForm of non-numeric elements at the second level.
  • Export["file.csv",expr] also works for structured input such as Dataset and TimeSeries.
  • See the reference pages for full general information on Import and Export.
  • ImportString and ExportString support the CSV format.

Notebook Interface

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

Elements

  • 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
    "Summary"summary of file size and dimensions
  • Data representation elements:
  • "Data"two-dimensional array
    {"Data",rows,cols}imports specified rows and columns
    "Grid"table data as a Grid object
    "RawData"two-dimensional array of strings
    {"RawData",rows,cols}imports raw data from specified rows and columns
    "Dataset"table data as a Dataset
  • Import and Export use the "Data" element by default.
  • Part specification rows and cols can be any of the following:
  • nnth row or column
    {n1,n2,}specific rows or columns ni
  • Dimensions elements:
  • "RowCount"number of rows
    "MaxColumnCount"maximum number of columns

Options

  • Import and Export options:
  • CharacterEncodingAutomaticraw character encoding used in the file
    "EmptyField"""how to represent empty fields
    "TextDelimiters""\""character used to delimit non-numeric fields
  • By default, Import attempts to interpret the data as "UTF8" encoded text. If any sequence of bytes stored in the file cannot be represented in "UTF8", Import uses "ISOLatin1" instead.
  • Export by default creates a "UTF8" encoded file.
  • Data fields containing commas and line separators are typically wrapped in double-quote characters. By default, Export uses double-quote characters as delimiters. Specify a different character using "TextDelimiters".
  • Double-quote characters delimiting text fields are not imported by default.
  • Import option:
  • "CurrencyTokens"{{"$", "£", "¥", "€"}, {"c", "¢", "p", "F"}}currency units to be skipped when importing numerical values
    "DateStringFormat"Nonedate format, given as a DateString specification
    "FillRows"Automaticwhether to fill all rows to the maximum column length
    "HeaderLines"0number of lines to assume as headers
    "IgnoreEmptyLines"Falsewhether to ignore empty lines
    "Numeric"Automaticwhether to import data fields as numbers if possible
    "SkipLines"0number of lines to skip at the beginning of the file
  • Possible settings for "HeaderLines" and "SkipLines" are:
  • nn rows to skip or to use as Dataset headers
    {rows,cols}rows and columns to skip or to use as headers
  • Import converts table entries formatted as specified by "DateStringFormat" to a DateObject.
  • Export options:
  • AlignmentNonehow data is aligned within table columns
    "TableHeadings"Noneheadings for table columns and rows
  • Possible settings for Alignment are None, Left, Center, and Right.
  • "TableHeadings" 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.

Examples

open allclose all

Basic Examples  (3)

Import a CSV file:

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

Read and plot all data from the file:

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

Export an array of expressions to CSV:

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

Export an array of expressions to CSV:

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

Scope  (5)

Import Elements  (13)

Import Options  (10)

Export Options  (6)

Applications  (1)

Possible Issues  (2)

See Also

"TSV"  "Table"  "XLS"  "Ini"

Introduced in 1999
(4.0)
| Updated in 2017
(11.2)