# Simple CSV/DSV importer

Slightly more than an inverse of this.

In: Multi-line DSV data and a single delimiter character. The DSV may be taken as a file, a filename, line-break separated string, list of strings, etc. All records have the same number of fields, and no field is empty. Data does not contain the delimiter character and there is no quoting or escaping mechanism.

Out: A data structure representing the DSV, e.g. a list of lists of strings or a matrix of strings.

Examples

["here is,some,sample","data,delimited,by commas"] and ",":
[["here is","some","sample"],["data","delimited","by commas"]]

["hello;\"","\";world","\";\""] and ";":
[["hello","\""],["\"","world"],["\"","\""]] (escapes because this example uses JSON)

["to be or not","that is the question"] and " ":
[["to","be","or","not"],["that","is","the","question"]]

• So just to clarify, we simply split each item at instances of the given char? – ETHproductions Feb 28 '17 at 19:42
• @ETHproductions That's right. – Adám Feb 28 '17 at 19:45
• How should we split the strings if the first or last character is the delimiter? ",for,example,this,string," – G B Mar 1 '17 at 10:28
• @GB no field is empty – Adám Mar 1 '17 at 10:35
• So we can assume it won't happen? – G B Mar 1 '17 at 10:42

# Jelly, 3 2 bytes

Dennis points out that while the 2 byte solution appears to not work, the dyadic link itself does, and that it is actually the way command line arguments are parsed that make it look that way.

ṣ€

Try It Online! - footer calls the function with left and right set explicitly, and formats as a grid*.

Exactly as the below, except splits at occurrences of the right argument rather than at sublists equal to the right argument.

œṣ€

The 3 byter - footer displays the result as a grid*.

A dyadic link (function) that takes the DSV list on the left and the delimiter on the right.

### How?

œṣ€ - Main link: list l, delimiter d
€ - for each item in l:
œṣ  -     split at occurrences of sublists equal to d

* As a full program the implicit output would simply "smush" together all the characters, so the footer of the TIO link calls the link as a dyad and uses G to format the result nicely.

• @Okx the implicit output would simply "smush" together all the characters – Adám Feb 28 '17 at 19:48
• @Okx Yes it is a function that returns a list. The footer is to override the implicit output that occurs when it is run as a full program. – Jonathan Allan Feb 28 '17 at 19:48

# Japt, 3 bytes

mqV

Test it online! (Uses the -Q flag to prettyprint the output)

mqV  // Implicit: U, V = inputs
m    // Map each item in U by the following function:
qV  //   Split the item at instances of V.
// Implicit: output result of last expression
• :O a JSGL beat MATL! – Downgoat Mar 1 '17 at 3:18

# Powershell, 25 22/23 bytes

Two Options, one just calls split on the first arg, using the second arg as a delim value.

$args[0]-split$args[1]

One byte longer, builtin to parse csvs, takes filename as first arg and delim as second.

ipcsv $args[0]$args[1]

-2 because it doesn't require the -Delimiter (-D) param, and will assume it by default.

sadly powershell cannot pass an array of two params, as it will assume they are both files, and will run the command against it twice, no other two-var input method is shorter than this as far as I can see, so this is likely the shortest possible answer.

ipcsv is an alias for Import-Csv, takes a file name as the first unnamed input, and the delim character as the second by default behavior.

Run against the example from the wiki page returns

PS C:\Users\Connor\Desktop> .\csvparse.ps1 'example.csv' ','

----     -----               -----
25 May   Bloggs, Fred        C
25 May   Doe, Jane           B
15 July  Bloggs, Fred        A
15 April Muniz, Alvin "Hank" A

# Python, 33 bytes

lambda a,c:[x.split(c)for x in a]

import Data.Lists
map.splitOn

Usage example: (map.splitOn) " " ["to be or not","that is the question"] -> [["to","be","or","not"],["that","is","the","question"]].

# 05AB1E, 5 bytes

vy²¡ˆ

Try it online!

Explanation:

v     For each element in the input array
y    Push the element
²   Push second input
¡  Split

## JavaScript, 26 bytes

x=>y=>x.map(n=>n.split(y))

Receives input in format (array of strings)(delimiter)

Try it online!

# Mathematica, 11 bytes

StringSplit

Builtin function taking two arguments, a list of strings and a character (and even more general than that). Example usage:

StringSplit[{"to be or not", "that is the question"}, " "]

yields

{{"to", "be", "or", "not"}, {"that", "is", "the", "question"}}

# MATLAB / Octave, 41 25 bytes

@(x,d)regexp(x,d,'split')

Creates an anonymous function named ans which accepts the first input as a cell array of strings and the second input as a string.

ans({'Hello World', 'How are you'}, ' ')

Try it Online

# Cheddar, 19 bytes

a->b->a=>@.split(b)

nice demonstration of looping abilities. I added new composition and f.op. blocks so that allows for interesting golfing. (=>:@.split) is supposed to work but it doesn't :(

# MATL, 1412 4 bytes

H&XX

Try it at MATL Online (the link has a modification at the end to show the dimensionality of the output cell array).

Explanation

% Implicitly grab the first input as a cell array of strings
% Implicitly grab the delimiter as a string
H       % Push the number literal 2 to the stack
&XX     % Split the input at each appearance of the delimiter
% Implicitly display the result

# CJam, 5 bytes

l~l./

Explanation:

l~     e#Input evaluated (as list)
l    e#Another line of input
./  e#Split first input by second

## Ruby using '-n', 17+1 = 18 bytes

p chomp.split *$* ### How it works • Input from file • separator is given as command line parameter • since we only have 1 parameter, *$* splats the string and we can use it as a parameter for the split function
• I tried to avoid chomp but any other solution seems to be longer than this.

# Rebol, 33 bytes

func[b s][map-each n b[split n s]

## GNU sed, 48 + 1(r flag) = 49 bytes

1h;1d
:
G
/,$/bp s:(.)(.*)\n\1:,\2: t :p;s:..$::

Try it online!

In sed there are no data types, but a natural representation of a list would be a collection of lines. As such, the input format consists of DSV records each on a separate line, with the delimiter present on the first line.

Explanation: by design, sed runs the script as many times as there are input lines

1h;1d                  # store delimiter, start new cycle
:                      # begin loop
G                      # append saved delimiter
/,$/bp # if delimiter is ',', skip replacements and go to printing s:(.)(.*)\n\1:,\2: # replace first occurrence of delimiter with ',' t # repeat :p;s:..$::             # print label: delete appended delimiter (implicit printing)

## REXX, 95 bytes

arg f d
do l=1 while lines(f)
n=linein(f)
do #=1 while n>''
parse var n w (d) n
o.l.#=w
end
end

Takes a filename and a delimiter as arguments, contents of file are put in stem o.

• Is all that whitespace really necessary? – Adám Mar 29 '17 at 8:03
• No, I only indented it for readability. The byte count is for unindented code. – idrougge Mar 29 '17 at 9:03
• Which flavour of REXX is this? – Adám Mar 29 '17 at 9:39
• I think it's pure ANSI REXX. I've only tested it with Regina. – idrougge Mar 29 '17 at 11:02
• On its way to TIO... – Adám Mar 29 '17 at 11:04

# APL (Dyalog), 4 bytes

In versions up to and including 15.0, this needs ⎕ML←3 which is default by many. From version 16.0 can just be replaced by for the same effect.

Takes separator as left argument and DSV as right argument.

≠⊂¨⊢

Try it online!

the inequalities (of the left argument and the right argument)

⊂¨ partition each

right argument

By partition is mean to remove all elements indicated by a corresponding zero in the left argument, and begin a new partition whenever a the corresponding number in the left argument is greater than its predecessor, i.e. on every one if the left argument is Boolean, as is the case here.

# R, 8 bytes (2 ways)

R has two builtin functions that meet the requirements of this challenge:

strsplit

takes a vector of strings and a separator, and returns a list of vectors of the separated strings.