csvkit icon indicating copy to clipboard operation
csvkit copied to clipboard

csvcut/csvgrep: Allow for numeric column names and column names containing hyphen (`--column-names`, `--column-indexes`)

Open fgregg opened this issue 10 years ago • 7 comments

I have data with a column names like

pre-k,k,01,02,03,04,05

where the numbers refer to grades. I can't select the 3rd grade column using csvcut

csvcut -c "k,03" returns the kindergarten row and the 2nd grade row (the fourth column)

fgregg avatar Dec 02 '15 22:12 fgregg

@onyxfish would you accept a PR that changed the behavior so that if the number was quoted it would interpret it as a string.

fgregg avatar Dec 15 '15 17:12 fgregg

Can you give an example of what the command would look like?

jpmckinney avatar Jan 23 '16 00:01 jpmckinney

I like to have a solution for this. (I've seen plenty of spreadsheets with year columns.) However, I don't want to rely on quote characters on the command line, as they have a particular meaning that may vary from platform to platform.

onyxfish avatar Jan 23 '16 00:01 onyxfish

echo "pre-k,k,01,02,03,04,05" | csvcut -c "k,03"

Expected:

k,03

Actual:

k,01

jpmckinney avatar Oct 11 '16 22:10 jpmckinney

What if we added explicit --column-names and --column-indices options to all tools (cut, grep, join, sort, stat) that accept --columns (-c) to give users an opportunity to be unambiguous?

jpmckinney avatar Jan 30 '17 03:01 jpmckinney

@jpmckinney , what is the status of the proposed --column-names/--column-indices options? This would solve another ambiguity: csvcut cannot handle column names that contain dashes, as in "age-mean" because it thinks the argument is a range (even for quoted column names); the error is the following:

Invalid range %s. Ranges must be two integers separated by a - or : character.

With --column-names there would be no ambiguity because csvcut would not even try to parse ranges.

daaugusto avatar Sep 08 '18 01:09 daaugusto

I've increased the issue's priority, so that I notice it next time I do a round of maintenance – but I can't make any promises about how soon I'll implement it. I accept pull requests, though!

jpmckinney avatar Sep 08 '18 04:09 jpmckinney