From 4ccaf79d3e02c45c113bc9245efb2532c6b4e340 Mon Sep 17 00:00:00 2001 From: dxts Date: Tue, 24 Nov 2026 09:18:16 +0000 Subject: [PATCH] Document `from csv`/`from tsv` reading every field as a string Follows nushell/nushell's change that makes type inference opt-in via `--infer` and deprecates `--no-infer` (nushell/nushell#13514). - working_with_strings: rewrite the "Options for from csv and from tsv" intro around the new default, show `--infer` instead of `--no-infer`, point at `into int` / `into float` / `detect type` for single columns, tie the `--trim` note to `--infer`, and update the `--flexible` output. - external_completers: drop `--no-infer` from the fish completer snippets and the bullet explaining it. - matrices: `from csv` no longer needs `--no-infer` for string cells. --- book/working_with_strings.md | 22 +++++++++++++--------- cookbook/external_completers.md | 5 ++--- lang-guide/chapters/matrices.md | 2 +- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/book/working_with_strings.md b/book/working_with_strings.md index 023d343b780..1d02d76613c 100644 --- a/book/working_with_strings.md +++ b/book/working_with_strings.md @@ -992,26 +992,30 @@ If a string is known to contain comma-separated, tab-separated or multi-space-se ### Options for `from csv` and `from tsv` -`from csv` turns a field into an int or a float when it parses as one. Every other field stays a string, including `true` and dates. `--no-infer` keeps every field a string, which also keeps leading zeros: +`from csv` reads every field as a string, so values such as `007`, `1E5` or `NaN` stay as written. `--infer` turns a field into an int or a float when it parses as one. Every other field stays a string, including `true` and dates: ```nu "id,price,active,since\n007,2.50,true,2024-01-02" | from csv | describe +# => table (stream) +"id,price,active,since\n007,2.50,true,2024-01-02" | from csv --infer | describe # => table (stream) -"id,price\n007,2.50" | from csv --no-infer -# => ╭───┬─────┬───────╮ -# => │ # │ id │ price │ -# => ├───┼─────┼───────┤ -# => │ 0 │ 007 │ 2.50 │ -# => ╰───┴─────┴───────╯ +"id,price\n007,2.50" | from csv --infer +# => ╭───┬────┬───────╮ +# => │ # │ id │ price │ +# => ├───┼────┼───────┤ +# => │ 0 │ 7 │ 2.50 │ +# => ╰───┴────┴───────╯ ``` +To convert only some columns, run [`into int`](/commands/docs/into_int.md), [`into float`](/commands/docs/into_float.md) or [`detect type`](/commands/docs/detect_type.md) on them, as in `from csv | update id { into int }`. `--no-infer` is deprecated: it asks for the default and warns on first use. + The other options describe the format of the text: - `--separator` sets the column separator. It takes one ASCII character, or four hex digits naming one, such as `001f` for the unit separator. - `--comment` names a character that marks a comment. Lines that start with it are skipped. - `--noheaders` treats the first line as data. The columns are then named `column0`, `column1` and so on. - `--quote` changes the quote character from `"`. A separator inside quotes is part of the field. By default, a quote inside a quoted field is written twice (`"say ""hi"""`). `--escape` names a character that escapes it instead, such as `\` for `"say \"hi\""`. -- `--trim` removes whitespace around the header names (`headers`), the fields (`fields`), or both (`all`). The default is `none`. Trimming happens before the number conversion, so a field written as ` 3` only becomes the int `3` when fields are trimmed. +- `--trim` removes whitespace around the header names (`headers`), the fields (`fields`), or both (`all`). The default is `none`. Trimming happens before `--infer`'s number conversion, so a field written as ` 3` only becomes the int `3` with `--infer --trim fields`. - `--flexible` allows rows with different numbers of fields. Without it, such a row is an error ("found record with 3 fields, but the previous record has 2 fields"). Extra fields get columns named after their position, and short rows lack the missing columns. ```nu @@ -1023,7 +1027,7 @@ The other options describe the format of the text: # => │ 1 │ pear │ 10 │ # => ╰───┴───────┴─────╯ "a,b\n1,2,3\n4" | from csv --flexible | to nuon -# => [{a: 1, b: 2, "column2": 3}, {a: 4}] +# => [{a: "1", b: "2", "column2": "3"}, {a: "4"}] ``` `from tsv` takes the same options, except `--separator`, because its separator is always a tab. diff --git a/cookbook/external_completers.md b/cookbook/external_completers.md index 5dc59b0d946..09110aa27cf 100644 --- a/cookbook/external_completers.md +++ b/cookbook/external_completers.md @@ -23,7 +23,7 @@ This completer will use [the fish shell](https://fishshell.com/) to handle compl ```nu let fish_completer = {|place| fish --command $"complete '--do-complete=($place.command | str replace --all "'" "\\'" | str join ' ')'" - | from tsv --flexible --noheaders --no-infer + | from tsv --flexible --noheaders | rename value description | update value {|row| let value = $row.value @@ -40,7 +40,6 @@ A couple of things to note on this command: - The fish completer will return lines of text, each one holding the `value` and `description` separated by a tab. The `description` can be missing, and in that case there won't be a tab after the `value`. If that happens, `from tsv` will fail, so we add the `--flexible` flag. - The output of the fish completer does not contain a header (name of the columns), so we add `--noheaders` to prevent `from tsv` from treating the first row as headers and later give the columns their names using `rename`. -- `--no-infer` is optional. `from tsv` will infer the data type of the result, so a numeric value like some git hashes will be inferred as a number. `--no-infer` will keep everything as a string. It doesn't make a difference in practice but it will print a more consistent output if the completer is run on its own. - Since fish only supports POSIX style escapes for file paths (`file\ name.txt`, etc.), file paths completed by fish will not be quoted or escaped properly on external commands. Nushell does not parse POSIX escapes, so we need to do this conversion manually such as by testing if the items are valid paths as shown in the example. To minimize the overhead of path lookups, we first check the string for common escape characters. If the string needs escaping, and it is a path on the filesystem, then the value is double-quoted. Also before double-quoting the file path we expand any ~ at the beginning of the path, so that completions continue to work. This simple approach is imperfect, but it should cover 99.9% of use cases. To use one of these completers, assign it to the config: @@ -126,7 +125,7 @@ This is an example of how an external completer definition might look like: ```nu let fish_completer = {|place| fish --command $"complete '--do-complete=($place.command | str replace --all "'" "\\'" | str join ' ')'" - | from tsv --flexible --noheaders --no-infer + | from tsv --flexible --noheaders | rename value description | update value {|row| let value = $row.value diff --git a/lang-guide/chapters/matrices.md b/lang-guide/chapters/matrices.md index 4e74d619383..0fd848692da 100644 --- a/lang-guide/chapters/matrices.md +++ b/lang-guide/chapters/matrices.md @@ -33,7 +33,7 @@ A table with a text column can't be converted. Select the numeric columns first: # => [[1.0], [2.0]] ``` -A string that holds a number, such as the cells of a CSV file read with `from csv --no-infer`, is converted to a float. A literal list of strings like `[["1" "2"]]` is rejected when the pipeline is parsed, because its type is known to be wrong. A flat list of numbers is rejected the same way. Wrap it in another list to get a matrix with one row, or see [one-dimensional matrices](#one-dimensional-and-larger-matrices) below. An empty list gives a matrix with the shape `[0, 0]`. +A string that holds a number, such as the cells of a CSV file read with `from csv`, is converted to a float. A literal list of strings like `[["1" "2"]]` is rejected when the pipeline is parsed, because its type is known to be wrong. A flat list of numbers is rejected the same way. Wrap it in another list to get a matrix with one row, or see [one-dimensional matrices](#one-dimensional-and-larger-matrices) below. An empty list gives a matrix with the shape `[0, 0]`. ### `matrix identity` and `matrix zeros`