# Shortest way to strip the commas from column n given a .csv file/input

Ok, so the input here is going to be a .csv file and so is the output. Now, given a .csv file/input like so

"apple","banana","apple,banana","orange"
"grape","orange","grape,orange","sprout"
# 0       1        2              3


strip out all commas for a given nth column's entries (index starts at 0). So, for the input above, the program would output a .csv file like so for n = 2:

"apple","banana","applebanana","orange"
"grape","orange","grapeorange","sprout"


If the particular column's entries do not have commas, then it does nothing. Could have any allowable/manageable number of columns, and if you somehow find that creating a program that takes more than one argument, say, a, b, c, ..., z like this

program(a=1,b=4,c=65, ..., z=0, mycsv.csv)


for respective columns 2, 5, and 66, which all strip out commas for each, to be shorter, then do that.

Shortest code measured in bytes wins, and it doesn't have to be a file that is output/input.

• Are double quotes allowed in a field? If yes, how are they escaped? – Arnauld Sep 24 '16 at 21:26
• This feels like a homework challenge under the guise of code-golf – Matt Sep 24 '16 at 22:55
• How can we accept the file as input? string array? file name? whatever suits us? – Matt Sep 24 '16 at 22:56
• Please clarify which characters can appear in the input fields. A test case that has commas outside the column we have to modify would also be helpful. – Dennis Sep 25 '16 at 15:25
• Does it has to be zero based? E.g. If the language or approach is 1-based? – Christian Rondeau Sep 27 '16 at 21:00

# 05AB1E, 27 bytes

U|vy“","“©¡vyNXQi","-}})®ý,


Explanation

U                           # store index in X
|v                         # for each line
y“","“©¡                 # split on ","
v          }     # for each in the split list
yNXQi","-}      # if it's index equals X, remove any commas
)    # wrap in list
®ý, # merge on "," and print


Try it online!

## JavaScript (ES6), 9593 90 bytes

Takes the content of the CSV file as its first parameter and the column index as its second parameter, with currying syntax.

let f =

s=>n=>s.split(/("[^"]*")/).map(c=>(c<' '?(k=0,c):k++-n*2?c:c.split,.join),k=-1).join

console.log(f(
"apple","banana","apple,banana","orange"
"grape","orange","grape,orange","sprout"
)(2));

• The question says it must take a file as input. – DanTheMan Sep 24 '16 at 22:30
• @DanTheMan - This is apparently not required anymore. – Arnauld Sep 25 '16 at 0:27
• Are the s= and the parens in the regex necessary? – ETHproductions Sep 25 '16 at 0:46
• @ETHproductions - My bad. The s= was left from a previous attempt. Thanks for noticing. I don't think I can get rid of the parens in the split, though. – Arnauld Sep 25 '16 at 0:49
• Oh, I didn't realize that you could capture groups in the split argument! That's a cool feature. – ETHproductions Sep 25 '16 at 1:24

# Ruby, 80 bytes

require'csv'
->n,x{CSV.read(x).map{|a|a[n].delete!',';a.to_csv(force_quotes:1)}}


A lambda that takes n and a file path argument, and returns a list of CSV rows.

• Would .tr!?,,'' work? – m-chrzan Sep 25 '16 at 19:26

# Python 2, 129126125989694 92 bytes

lambda n,f:''.join(c*((r[:i].count('"')-n*2)*','!=c)for r in open(f)for i,c in enumerate(r))


This is a lambda expression; to use it, prefix lambda with s=.

Iterates through each character of each line, filtering characters based on whether it's a comma and it's in a double-quoted string.

Now doesn't use the csv module at all!

Example run:

>>> s=lambda n,f:''.join(c*((r[:i].count('"')-n*2)*','!=c)for r in open(f)for i,c in enumerate(r))
>>> print s(2, 'test.csv')
"apple","banana","applebanana","orange"
"grape","orange","grapeorange","sprout"


# Sh + GNU Sed GNU Sed, 36 28 + 1 bytes

Run with -r flag.

### Explanation

• qq starts recording macro q
• /\v"(,"|$)<cr> searches for the next ending quote or end if line. Preceded by the input column number, it finds the nth occurrence • vF" visually selects the previous quote • ":s/\%V,//<cr> substitutes in the visual selection (\%V) every comma by nothing • <cr> goes to the next line • @qq@q makes the macro recursive, stops it and then play it until the end of the file. ### Gotchas • If a column only contains commas, it will be detected as a column end and will break. This is a gray zone, I will adapt the answer is the question adds this to the test cases. # Haskell, 56 bytes n!x=Math.FFT.Base.adjust(filter(/=','))n.read$'[':x++"]"


Without adjust you'd have to do something crazy like this:

n#(a,b)|a==n|filter(/=',')b|1<2=b; .. (n#)=<<zip ..


# R, 59 bytes

 function(f,n){x=read.csv(f,h=0);x[,n]=gsub(",","",x[,n]);x}


A simple function that takes f, the location of the csv file, and n, the column number to replace. Reads in the file, removes all commas from the nth column, returns the resulting data frame. R indexes from 1, not from 0.

# PHP, 81 bytes

while($r=fgetcsv(STDIN)){$r[$n=argv[1]]=strtr($r[$n],",","");fputcsv(STDOUT,$r);}


Run with php -r '<code>' <n> < <csvsource> > <csvtarget>.