In this challenge, given a CSV file as a string, you'll return the data contained as a 2d array of strings.
Spec:
- The input consists of one or more records, delimited with
\r\n
(CRLF),\n
(line feed), or some other reasonable newline sequence of your choice - A record consists of one or more fields, delimited with
,
(commas),;
(semicolons),\t
(tabs), or some other reasonable delimiter of your choice. You may assume each record contains the same number of fields - A field can either be quoted or un-quoted. An un-quoted field will not contain a field delimiter, newline sequence, or quote character, but may otherwise be an arbitrary non-empty string (within reason; you can assume things like invalid UTF-8, null characters, or unpaired surrogates won't appear if doing so is justifiable)
- A quoted string will be delimited on both sides by a quote character, which will be
"
(double quotes),'
(single quotes),`
(backticks), or some other reasonable delimiter of your choice. This character will be escaped within the string by being repeated twice (the string unquoteda"bc
would be represented as"a""bc"
). Quoted strings may contain field delimiters, newline sequences, escaped quote characters, and any other sequences of characters which may appear in an unquoted field. Quoted strings will always be non-empty - You may choose to ignore the first record (as if it is a header record)
Output:
You may represent a 2d array in any reasonable format (excluding CSV or any of it's variants for obvious reasons).
Scoring:
This is code-golf, so shortest answer (in bytes) per language wins.
Test cases:
In:
1,2
abc,xyz
Out:
[["1","2"],["abc","xyz"]]
In:
"abc",xyz
"a""bc",""""
",",";"
"
"," "
Out:
[["abc","xyz"],["a\"bc","\""],[",",";"],["\n"," "]]
In:
"\
,"""
Out:
[["\\\n,\""]]
$$$$$$$$
",","
. I think that one of them should be parsed as an unquoted"
, but I'm not sure which. \$\endgroup\$