ImportString¶
Parses a string as a supported format, such as CSV.
Options¶
"CharacterEncoding"— encoding of the input string (default"UTF8")."Numeric"— ifTrue, 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"]:
"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:
"List" reads one field per line, typed the way a CSV field is:
A JSON number written without a decimal point stays exact:
"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 '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:
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:
The row count follows that decision, while "Data" always holds every row:
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"|>
A field written true or false is read as a boolean: