Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions book/working_with_strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<id: string, price: string, active: string, since: string> (stream)
"id,price,active,since\n007,2.50,true,2024-01-02" | from csv --infer | describe
# => table<id: int, price: float, active: string, since: string> (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
Expand All @@ -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.
Expand Down
5 changes: 2 additions & 3 deletions cookbook/external_completers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lang-guide/chapters/matrices.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
Loading