Skip to content

ImportString

Parses a string as a supported format, such as CSV.

$ wo 'ImportString["1,2,3\n4,5,6", "CSV"]'
{{1, 2, 3}, {4, 5, 6}}

Options

  • "CharacterEncoding" — encoding of the input string (default "UTF8").
  • "Numeric" — if True, numeric fields are converted to numbers.
  • "HeaderLines" — number of header lines to skip (format-dependent).
  • "Delimiter" — CSV/TSV field delimiter.

An empty field has no value, which is reported as Missing["NotAvailable"]:

$ wo 'ImportString["1,,3", "CSV"]'
{{1, Missing[NotAvailable], 3}}

"Numeric" -> False keeps every field as the string it was written as:

$ wo 'ToString[ImportString["a,b\n1,2", "CSV", "Numeric" -> False], InputForm]'
{{"a", "b"}, {"1", "2"}}

An empty document has no rows to read, so the tabular formats fail:

$ wo 'ImportString["", "CSV"]'

Import::fmterr: Cannot import data as CSV format.
$Failed

"List" reads one field per line, typed the way a CSV field is:

$ wo 'ToString[ImportString["1\nabc", "List"], InputForm]'
{1, "abc"}

A JSON number written without a decimal point stays exact:

$ wo 'ToString[ImportString["{\"a\": 1e3}", "JSON"], InputForm]'
{"a" -> 1000}
$ wo 'ToString[ImportString["{\"a\": 1e-3}", "JSON"], InputForm]'
{"a" -> 1/1000}

"XML" reads a document into symbolic XML — XMLObject["Document"] holding the prolog, the root XMLElement[tag, {attributes}, {children}] and the epilog. Whitespace between elements is layout rather than text, and comments carry no content:

$ wo 'ToString[ImportString["<a x=\"1\"><b>2</b></a>", "XML"], InputForm]'
XMLObject["Document"][{}, XMLElement["a", {"x" -> "1"}, {XMLElement["b", {}, {"2"}]}], {}]
$ wo 'ImportString["not xml", "XML"]'

Import::nfprserr: invalid document structure at line: 1 character: 1 in input string.
$Failed

A prefixed name carries its namespace URI, written {uri, local}, and the xmlns declarations become attributes of their own namespace:

$ wo 'ToString[ImportString["<ns:a xmlns:ns=\"u\"><ns:b>t</ns:b></ns:a>", "XML"], InputForm]'
XMLObject["Document"][{}, XMLElement[{"u", "a"}, {{"http://www.w3.org/2000/xmlns/", "ns"} -> "u"}, {XMLElement[{"u", "b"}, {}, {"t"}]}], {}]

A {format, element} spec reads one part of the document:

$ wo 'ToString[ImportString["<a><b>t1</b><b>t2</b></a>", {"XML", "Tags"}], InputForm]'
{"a", "b"}
$ wo 'ImportString["<a><b>t1</b><b>t2</b></a>", {"XML", "Plaintext"}]'
t1
t2
$ wo 'ImportString["<a>1</a>", {"XML", "Bogus"}]'

Import::noelem: The Import element "Bogus" is not present when importing as XML.
$Failed

The tabular formats take an element as well:

$ wo 'ToString[ImportString["1,2\n3,4", {"CSV", "Data"}], InputForm]'
{{1, 2}, {3, 4}}

The first row names the columns only when it reads as labels: text on top and typed data below. A file of numbers, or one whose columns all read as text, has no labels at all:

$ wo 'ToString[ImportString["a,b\n1,2", {"CSV", "ColumnLabels"}], InputForm]'
{"a", "b"}
$ wo 'ToString[ImportString["1,2\n3,4", {"CSV", "ColumnLabels"}], InputForm]'
None

The row count follows that decision, while "Data" always holds every row:

$ wo 'ImportString["a,b\n1,2\n3,4", {"CSV", "RowCount"}]'
2
$ wo 'ToString[ImportString["1,2\n3,4", {"CSV", "RowCount"}], InputForm]'
2

Column types are keyed by the labels when there are any:

$ wo 'ToString[ImportString["a,b,c\n1,2,3\n", {"CSV", "ColumnTypes"}], InputForm]'
<|"a" -> "Integer64", "b" -> "Integer64", "c" -> "Integer64"|>
$ wo 'ToString[ImportString["1\n2.5", {"CSV", "ColumnTypes"}], InputForm]'
{"Real64"}

A field written true or false is read as a boolean:

$ wo 'ToString[ImportString["true,false", "CSV"], InputForm]'
{{True, False}}