OpenersPrimaryExamplesSection(Spacer1)Examples  (48)

OpenersExampleSection0(Spacer1)Basic Examples  (3)

Import a CSV file:

Import["ExampleData/financialtimeseries.csv", "Data"] // Short

Read and plot all data from the file:

DateListPlot[Import["ExampleData/financialtimeseries.csv"]]

Import a CSV file as a (RefLink[Dataset, paclet:ref/Dataset]) with headers:

Import["ExampleData/TreesOwnedByTheCityOfChampaign.csv","Dataset","HeaderLines"->1]

Export an array of expressions to CSV:

ExportString[{{"a",1},{"b",2},{"c",3}},"CSV"]

ImportString[%]

OpenersExampleSection1(Spacer1)Scope  (5)

Export a (RefLink[Dataset, paclet:ref/Dataset]):

dataset=Dataset[{
<|"a"->1,"b"->"x"|>,
<|"a"->2,"b"->"y"|>,
<|"a"->3,"b"->"z"|>,
<|"a"->4,"b"->"x"|>,
<|"a"->5,"b"->"y"|>,
<|"a"->6,"b"->"z"|>}]

input=ExportString[dataset, "CSV"]

Use (RefLink[Values, paclet:ref/Values]) to remove headers from a dataset:

ExportString[Values[dataset], "CSV"]

Use the "HeaderLines" option to import table headers:

ImportString[input,{"CSV","Dataset"}, "HeaderLines" -> 1]

Export a (RefLink[TimeSeries, paclet:ref/TimeSeries]):

v={2,1,6,5,7,4};
t={1,2,5,10,12,15};

ts=TimeSeries[v,{t}]

ExportString[ts,"CSV"]

Export an (RefLink[EventSeries, paclet:ref/EventSeries]):

v={2,1,6,5,7,4};
t={1,2,5,10,12,15};

es=EventSeries[v,{t}]

ExportString[es,"CSV"]

Export a (RefLink[QuantityArray, paclet:ref/QuantityArray]):

qa=QuantityArray[{{1.4,2.3},{2.8,2.7},{4.2,3.5}},{"Seconds","Meters"}]

ExportString[qa, "CSV"]

Ragged arrays are automatically padded on the right and converted to full arrays:

ExportString[{a,{d,e,f}},"CSV"]

ImportString[%,"CSV"]

OpenersExampleSection2(Spacer1)Import Elements  (17)

OpenersExampleSubsection0(Spacer1)"Data"  (6)

Import a CSV file as a 2D list of values:

Import["ExampleData/financialtimeseries.csv", "Data"]//Short

This is also the default element:

Import["ExampleData/financialtimeseries.csv"]//Short

Import a single row from a CSV file:

Import["ExampleData/financialtimeseries.csv", {"Data",5}]

Import some specific rows from a CSV file:

Import["ExampleData/financialtimeseries.csv", {"Data",{1,5,5}}]

Import the first 10 rows of a CSV file:

Import["ExampleData/financialtimeseries.csv", {"Data",1;;10}]

Import a single row and column from a CSV file:

Import["ExampleData/financialtimeseries.csv", {"Data",5,2}]

Import a single column from a CSV file:

Import["ExampleData/financialtimeseries.csv", {"Data",All,2}]//Short

OpenersExampleSubsection1(Spacer1)"Dataset"  (3)

Import a CSV file as a (RefLink[Dataset, paclet:ref/Dataset]):

Import["ExampleData/TreesOwnedByTheCityOfChampaign.csv", "Dataset"]

Use "HeaderLines" to use the first row as column headers:

Import["ExampleData/TreesOwnedByTheCityOfChampaign.csv", "Dataset","HeaderLines"->1]

Use "SkipLines" to only import the data of interest:

Import["ExampleData/TreesOwnedByTheCityOfChampaign.csv", "Dataset","SkipLines"->1]

OpenersExampleSubsection2(Spacer1)"Dimensions"  (1)

Get the dimensions from a CSV file:

Import["ExampleData/financialtimeseries.csv", "Dimensions"]

If all rows in the file do not have the same number of columns, the maximum number of columns is used:

ImportString["1\n1,2\n1,2,3\n1,2,3,4\n1,2,3,4,5",{"CSV","Dimensions"}]

OpenersExampleSubsection3(Spacer1)"Grid"  (1)

Import CSV data as a (RefLink[Grid, paclet:ref/Grid]):

ImportString["a,b,c\nd,e,f\ng,h,i", {"CSV", "Grid"}]

OpenersExampleSubsection4(Spacer1)"MaxColumnCount"  (1)

Get the maximum number of columns from a CSV file:

ImportString["1\n1,2\n1,2,3\n1,2,3,4\n1,2,3,4,5", {"CSV","MaxColumnCount"}]

OpenersExampleSubsection5(Spacer1)"RawData"  (3)

Import CSV data as raw strings:

ImportString["1,2,3\na,b,c", {"CSV","RawData"}]//InputForm

Compare to "Data":

ImportString["1,2,3\na,b,c", {"CSV","Data"}]//InputForm

By default for "RawData", "Numeric"->(RefLink[False, paclet:ref/False]) is used:

ImportString["1,2,3\na,b,c", {"CSV","RawData"}]//InputForm

Use "Numeric"->(RefLink[True, paclet:ref/True]):

ImportString["1,2,3\na,b,c", {"CSV","RawData"},"Numeric"->True]//InputForm

By default for "RawData", "FillRows"->(RefLink[True, paclet:ref/True]) is used:

ImportString["1,2,3\na,b", {"CSV","RawData"}]//InputForm

Use "FillRows"->(RefLink[False, paclet:ref/False]):

ImportString["1,2,3\na,b", {"CSV","RawData"},"FillRows"->False]//InputForm

OpenersExampleSubsection6(Spacer1)"RowCount"  (1)

Get the maximum number of columns from a CSV file:

ImportString["1\n1,2\n1,2,3\n1,2,3,4\n1,2,3,4,5", {"CSV","RowCount"}]

OpenersExampleSubsection7(Spacer1)"Summary"  (1)

Summary of a CSV file:

Import["ExampleData/financialtimeseries.csv", "Summary"]

OpenersExampleSection4(Spacer1)Import Options  (10)

OpenersExampleSubsection8(Spacer1)CharacterEncoding  (1)

The character encoding can be set to any value from (RefLink[$CharacterEncodings, paclet:ref/$CharacterEncodings]):

input="Ã©";

ImportString[input, "CSV", CharacterEncoding -> "UTF8"]

ImportString[input, "CSV", CharacterEncoding -> "ISOLatin1"]

OpenersExampleSubsection9(Spacer1)"CurrencyTokens"  (1)

Currency tokens are automatically skipped:

ImportString["123c", "CSV"]

Use "CurrencyTokens"->(RefLink[None, paclet:ref/None]) to include all currency tokens:

ImportString["123c", "CSV", "CurrencyTokens"-> None]

OpenersExampleSubsection10(Spacer1)"DateStringFormat"  (1)

Convert dates to a (RefLink[DateObject, paclet:ref/DateObject]) using the date format specified:

ImportString["01/19/00 \n 05/18/02 \n 12/14/04","CSV", "DateStringFormat" -> {"Month","/","Day","/","YearShort"}]

By default, no conversion is happening:

ImportString["01/19/00 \n 05/18/02 \n 12/14/04","CSV"]

OpenersExampleSubsection11(Spacer1)"EmptyField"  (1)

Specify a default value for empty fields in CSV data:

ImportString["a,,c,d", "CSV","EmptyField"-> "XXXX"]

OpenersExampleSubsection12(Spacer1)"FillRows"  (1)

For the "Data" element, row lengths are automatically preserved:

input="a\nb,c\nd";

ImportString[input, {"CSV", "Data"}]

Pad rows:

ImportString[input, {"CSV", "Data"},"FillRows"->True,"EmptyField"->"XXXX"]

For the "RawData" element, a full array is imported by default:

ImportString[input, {"CSV", "RawData"},"EmptyField"->"XXXX"]

OpenersExampleSubsection13(Spacer1)"HeaderLines"  (1)

By default, no row or column is assumed to be a header line:

ImportString["a,b,c\nd,e,f\ng,h,i",{"CSV","Dataset"}]

Specify column headers:

ImportString["a,b,c\nd,e,f\ng,h,i",{"CSV","Dataset"}, "HeaderLines" -> 1]

Specify row headers:

ImportString["a,b,c\nd,e,f\ng,h,i",{"CSV","Dataset"}, "HeaderLines" -> {0,1}]

Specify row and column headers:

ImportString["a,b,c\nd,e,f\ng,h,i",{"CSV","Dataset"}, "HeaderLines" -> {1,1}]

OpenersExampleSubsection14(Spacer1)"IgnoreEmptyLines"  (1)

Use "IgnoreEmptyLines" to remove lines with no data from the imported data:

input = "a\nb\n\nd";

ImportString[input, {"CSV", "Data"}, "IgnoreEmptyLines" -> True]

ImportString[input, {"CSV", "Data"}, "IgnoreEmptyLines" -> False]

OpenersExampleSubsection15(Spacer1)"Numeric"  (1)

Use "Numeric"->(RefLink[True, paclet:ref/True]) to interpret numbers:

ImportString["1,2,3\na,b,c", {"CSV","RawData"},"Numeric"->True]//InputForm

By default, everything imports as strings:

ImportString["1,2,3\na,b,c", {"CSV","RawData"}]//InputForm

OpenersExampleSubsection16(Spacer1)"SkipLines"  (1)

CSV files may include a comment line:

ImportString["comment\na,b,c\nd,e,f\ng,h,i",{"CSV", "Dataset"},"FillRows"->True]

Skip the comment line:

ImportString["comment\na,b,c\nd,e,f\ng,h,i",{"CSV", "Dataset"},"SkipLines"-> 1]

Skip the comment line, and use the next line as a (RefLink[Dataset, paclet:ref/Dataset]) header:

ImportString["comment\na,b,c\nd,e,f\ng,h,i",{"CSV", "Dataset"},"SkipLines"-> 1, "HeaderLines" -> 1]

OpenersExampleSubsection17(Spacer1)"TextDelimiters"  (1)

The default text delimiter is a double-quote:

ImportString["\"a\nb\",c,d", {"CSV", "Data"}]

A different character can be used as the text delimiter:

ImportString["'a\nb',c,d", {"CSV", "Data"}, "TextDelimiters" -> "'"]

OpenersExampleSection5(Spacer1)Export Options  (7)

OpenersExampleSubsection18(Spacer1)Alignment  (1)

By default, no additional characters are added for any alignment:

ExportString[{{10, 1000000, 100}, {1, 10, 10000}}, "CSV"]

Left-align column values:

ExportString[{{10, 1000000, 100}, {1, 10, 10000}}, "CSV",Alignment-> Left]

Center-align column values:

ExportString[{{10, 1000000, 100}, {1, 10, 10000}}, "CSV",Alignment-> Center]

OpenersExampleSubsection19(Spacer1)CharacterEncoding  (1)

The character encoding can be set to any value from (RefLink[$CharacterEncodings, paclet:ref/$CharacterEncodings]):

input="é";

ExportString[input, "CSV", CharacterEncoding -> "UTF8"]

ExportString[input, "CSV", CharacterEncoding -> "ISOLatin1"]

OpenersExampleSubsection20(Spacer1)"EmptyField"  (1)

By default, empty elements are exported as empty strings:

ExportString[{{1,Null,2,3},{4,5,6}}, "CSV"]

Specify a different value for empty elements:

ExportString[{{1,Null,2,3},{4,5,6}}, "CSV", "EmptyField" -> "XXXX"]

OpenersExampleSubsection21(Spacer1)"FillRows"  (1)

By default, a full array is exported:

ExportString[{{1}, {1.0,2.0}, {"one","two","three"}},"CSV"]

Use "FillRows"->(RefLink[False, paclet:ref/False]) to preserve row lengths:

ExportString[{{1}, {1.0,2.0}, {"one","two","three"}},"CSV","FillRows"-> False]

OpenersExampleSubsection22(Spacer1)"TableHeadings"  (1)

Export data using custom column headers:

ExportString[{{1,2,3},{4,5,6}},"CSV","TableHeadings"-> {a,b,c}]

Export data using custom column and row headers:

ExportString[{{a,b,c},{d,e,f}},"CSV","TableHeadings"-> {{1,2},{3,4,5}}]

Export data using successive integer labels for columns and rows:

ExportString[{{a,b,c},{d,e,f}},"CSV","TableHeadings"-> Automatic,Alignment->Center]

OpenersExampleSubsection23(Spacer1)"TextDelimiters"  (2)

The default text delimiter for non-numeric elements is a double-quote:

ExportString[{{"a\nb","c","d"}}, {"CSV", "Data"}]

Specify a different delimiter character:

ExportString[{{"a\nb","c","d"}}, {"CSV", "Data"}, "TextDelimiters" -> "*"]

Use "TextDelimiters"->"" to disable any text delimiters:

ExportString[{{"a\nb","c","d"}}, "CSV", "TextDelimiters" -> ""]

By default, numeric elements do not get any delimiter characters:

ExportString[{{1,2,3},{a,b,c}}, "CSV"]

OpenersExampleSection6(Spacer1)Applications  (1)

Export a list of European countries and their populations to a CSV file:

list=EntityValue[(EntityClass["Group of 7", EntityClass["Country","GroupOf7"], "EntityClass[\"Country\", \"GroupOf7\"]", "countries"]),{"Name","Area","Population",  "GDP", "CurrencyUnit"}]

Export["G7.csv",list,TableHeadings->{"Name","Area","Population",  "GDP", "Currency Unit"}]

Import the data back and convert to expressions:

ToExpression/@Import["G7.csv","Dataset", "HeaderLines"-> 1]

OpenersExampleSection7(Spacer1)Possible Issues  (5)

Some CSV data generated from older versions of the Wolfram Language may have incorrectly delimited text fields and will not import as expected in Version 11.2:

TextGrid[ImportString["11,12,13\n21,Missing[\"Uncertain\", {\"giant\", \"subgiant\"}],23\n31,32,33","CSV"],Frame->All]

Using "TextDelimiters"->"" will give the previously expected result:

TextGrid[ImportString["11,12,13\n21,Missing[\"Uncertain\", {\"giant\", \"subgiant\"}],23\n31,32,33","CSV","TextDelimiters"->""],Frame->All]

By default, a full array is exported using a double-quote for "TextDelimiters":

ExportString[{{1}, {1.0,2.0}, {"one","two","three"}},"CSV"]

Use "TextDelimiters"->"" and "FillRows"->(RefLink[False, paclet:ref/False]) to export CSV data similar to versions prior to 11.2:

ExportString[{{1}, {1.0,2.0}, {"one","two","three"}},"CSV", "TextDelimiters"->"", "FillRows" -> False]

Entries of the format "nnnDnnn" or "nnnEnnn" are interpreted as numbers with scientific notation:

ImportString["2a3,2b2,2c3,2d3,2e3,2f3,2g3","CSV"]

Use the "Numeric" option to override this interpretation:

ImportString["2a3,2b2,2c3,2d3,2e3,2f3,2g3","CSV","Numeric"->False]

The top-left corner of data is lost when importing a (RefLink[Dataset, paclet:ref/Dataset]) with row and column headers:

ImportString["a,b,c\nd,e,f\ng,h,i",{"CSV","Dataset"}, "HeaderLines" -> {1,1}]

(RefLink[Dataset, paclet:ref/Dataset]) may look different depending on the dimensions of the data:

ImportString["a,b,c\nd,e,f\ng,h,i",{"CSV","Dataset"}, "HeaderLines" ->{0,1}]

Dimensions[%]

ImportString["a,b,c,d,e,f,g,h\ni,j,k,l,m,n,o,p\nq,r,s,t,u,v,w,x",{"CSV","Dataset"}, "HeaderLines" ->{0,1}]

Dimensions[%]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {Related Formats:  (RefLink[TSV, paclet:ref/format/TSV])  ▪  (RefLink[Table, paclet:ref/format/Table])  ▪  (RefLink[XLS, paclet:ref/format/XLS])  ▪  (RefLink[Ini, paclet:ref/format/Ini])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Basic Formats, paclet:guide/BasicFormats])}, {▪(RefLinkPlain[Importing & Exporting, paclet:guide/ImportingAndExporting])}, {▪(RefLinkPlain[Listing of All Formats, paclet:guide/ListingOfAllFormats])}, {▪(RefLinkPlain[List Manipulation, paclet:guide/ListManipulation])}, {▪(RefLinkPlain[Matrices and Linear Algebra, paclet:guide/MatricesAndLinearAlgebra])}, {▪(RefLinkPlain[Numerical Data, paclet:guide/NumericalData])}, {▪(RefLinkPlain[Numerical Data Formats, paclet:guide/NumericalDataFormats])}, {▪(RefLinkPlain[Statistics, paclet:guide/Statistics])}, {▪(RefLinkPlain[Tabular & Spreadsheet Formats, paclet:guide/TabularAndSpreadsheetFormats])}})}})