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 & 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,subelem1,}] imports subelements subelemi, 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.

Import 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
    "Grid"table data as a Grid object
    "RawData"two-dimensional array of strings
    "Dataset"table data as a Dataset
  • Import and Export use the "Data" element by default.
  • Subelements for partial data import for any element elem can take row and column specifications in the form {elem,rows,cols}, where rows and cols can be any of the following:
  • nnth row or column
    -ncounts from the end
    n;;mfrom n through m
    n;;m;;sfrom n through m with steps of s
    {n1,n2,}specific rows or columns ni
  • Metadata elements:
  • "Dimensions"a list of number of rows and maximum number of columns
    "MaxColumnCount"maximum number of columns
    "RowCount"number of rows

Options

  • Import and Export options:
  • "EmptyField"""how to represent empty fields
    "TextDelimiters""\""character used to delimit non-numeric fields
  • 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 options:
  • CharacterEncoding"UTF8ISOLatin1"raw character encoding used in the file
    "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 rows to the max column length
    "HeaderLines"0number of lines to assume as headers
    "IgnoreEmptyLines"Falsewhether to ignore empty lines
    "NumberPoint""."decimal point string
    "Numeric"Automaticwhether to import data fields as numbers if possible
    "SkipLines"0number of lines to skip at the beginning of the file
  • 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.
  • With CharacterEncoding -> Automatic, Import attempts to infer the character encoding 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
    CharacterEncoding"UTF8"raw character encoding used in the file
    "FillRows"Falsewhether to fill rows to the max column length
    "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 all close 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]=

Import a CSV file as a Dataset with headers:

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

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  (17)

Import Options  (10)

Export Options  (7)

Applications  (1)

Possible Issues  (5)

Introduced in 1999
(4.0)
|
Updated in 2019
(12.0)