nushell icon indicating copy to clipboard operation
nushell copied to clipboard

`from csv`: Combination of `--flexible` and `--noheaders`

Open PigeonF opened this issue 1 year ago • 1 comments

Related problem

Currently, when using from csv with the --flexible and --noheaders flags, the first row determines the amount of columns. I would like some way to instead use the maximum amount of columns, so that no entries are discarded (I am using the --flexible and --noheaders flags in the first place because I do not know the number of columns in advance).

That is, I would like

$ "foo\nbar,baz" | from csv --flexible --noheaders
╭───┬─────────╮
│ # │ column1 │
├───┼─────────┤
│ 0 │ foo     │
│ 1 │ bar     │
╰───┴─────────╯

to instead return

$ "foo\nbar,baz" | from csv --flexible --noheaders
╭───┬─────────┬─────────╮
│ # │ column1 │ column2 │
├───┼─────────┼─────────┤
│ 0 │ foo     │         │
│ 1 │ bar     │ baz     │
╰───┴─────────┴─────────╯

Describe the solution you'd like

When using from csv in combination with --flexible and --noheaders (or some other additional flag), the returned table should return the full amount of columns, and not a version trimmed based on the number of columns from the first row.

Describe alternatives you've considered

I thought of using lines | each { from csv --noheaders | into record } intead, which works mostly fine, but breaks multiline values.

Compare

$ "\"bar\nbaz\"\nfoo" | from csv --flexible --noheaders
╭───┬─────────╮
│ # │ column1 │
├───┼─────────┤
│ 0 │ bar     │
│   │ baz     │
│ 1 │ foo     │
╰───┴─────────╯
$ "\"bar\nbaz\"\nfoo" | lines | each { from csv --noheaders | into record }
╭───┬─────────╮
│ # │ column1 │
├───┼─────────┤
│ 0 │ bar     │
│ 1 │ baz"    │
│ 2 │ foo     │
╰───┴─────────╯

Additional context and details

No response

PigeonF avatar Jun 11 '24 13:06 PigeonF

Seems like that's the way --flexible --no-headers should work anyway. So, this might be a bug.

fdncred avatar Jun 11 '24 13:06 fdncred

I have the same problem, I didn't expect --flexible option to behave this way

LucaCiucci avatar Oct 31 '24 15:10 LucaCiucci

Fixed by #14399.

PigeonF avatar Dec 25 '24 23:12 PigeonF