2020-05-04 23:01:31 +02:00
# from csv
2019-10-25 19:52:40 +02:00
2020-03-13 18:23:41 +01:00
Converts csv data into table. Use this when nushell cannot determine the input file extension.
2019-10-25 19:52:40 +02:00
## Example
2019-11-08 14:11:04 +01:00
2020-06-23 20:21:47 +02:00
Let's say we have the following file:
2019-11-08 14:11:04 +01:00
2019-10-25 19:52:40 +02:00
```shell
> cat pets.txt
animal, name, age
cat, Tom, 7
dog, Alfred, 10
chameleon, Linda, 1
```
2020-06-23 20:21:47 +02:00
`pets.txt` is actually a .csv file but it has the .txt extension, `open` is not able to convert it into a table:
2019-10-25 19:52:40 +02:00
```shell
> open pets.txt
animal, name, age
cat, Tom, 7
dog, Alfred, 10
chameleon, Linda, 1
```
2020-06-23 20:21:47 +02:00
To get a table from `pets.txt` we need to use the `from csv` command:
2019-10-25 19:52:40 +02:00
```shell
2020-05-04 23:01:31 +02:00
> open pets.txt | from csv
2019-10-25 19:52:40 +02:00
━━━┯━━━━━━━━━━━┯━━━━━━━━━┯━━━━━━
# │ animal │ name │ age
───┼───────────┼─────────┼──────
0 │ cat │ Tom │ 7
1 │ dog │ Alfred │ 10
2 │ chameleon │ Linda │ 1
━━━┷━━━━━━━━━━━┷━━━━━━━━━┷━━━━━━
```
2020-06-23 20:21:47 +02:00
To ignore the csv headers use `--headerless` :
2019-11-08 14:11:04 +01:00
2019-10-25 19:52:40 +02:00
```shell
━━━┯━━━━━━━━━━━┯━━━━━━━━━┯━━━━━━━━━
# │ Column1 │ Column2 │ Column3
───┼───────────┼─────────┼─────────
0 │ dog │ Alfred │ 10
1 │ chameleon │ Linda │ 1
━━━┷━━━━━━━━━━━┷━━━━━━━━━┷━━━━━━━━━
```
2020-06-23 20:21:47 +02:00
To split on a character other than ',' use `--separator` :
2019-11-08 14:11:04 +01:00
```shell
> open pets.txt
animal; name; age
cat; Tom; 7
dog; Alfred; 10
chameleon; Linda; 1
```
```shell
2020-05-04 23:01:31 +02:00
> open pets.txt | from csv --separator ';'
2019-11-08 14:11:04 +01:00
━━━┯━━━━━━━━━━━┯━━━━━━━━━┯━━━━━━
# │ animal │ name │ age
───┼───────────┼─────────┼──────
0 │ cat │ Tom │ 7
1 │ dog │ Alfred │ 10
2 │ chameleon │ Linda │ 1
━━━┷━━━━━━━━━━━┷━━━━━━━━━┷━━━━━━
```
2020-05-04 23:01:31 +02:00
To use this command to open a csv with separators other than a comma, use the `--raw` switch of `open` to open the csv, otherwise the csv will enter `from csv` as a table split on commas rather than raw text.
2019-11-08 14:11:04 +01:00
```shell
> mv pets.txt pets.csv
2020-05-04 23:01:31 +02:00
> open pets.csv | from csv --separator ';'
2019-11-08 14:11:04 +01:00
error: Expected a string from pipeline
- shell:1:16
2020-05-04 23:01:31 +02:00
1 | open pets.csv | from csv --separator ';'
2019-11-08 14:11:04 +01:00
| ^^^^^^^^ requires string input
- shell:1:0
2020-05-04 23:01:31 +02:00
1 | open pets.csv | from csv --separator ';'
2019-11-08 14:11:04 +01:00
| value originates from here
2020-05-04 23:01:31 +02:00
> open pets.csv --raw | from csv --separator ';'
2019-11-08 14:11:04 +01:00
━━━┯━━━━━━━━━━━┯━━━━━━━━━┯━━━━━━
# │ animal │ name │ age
───┼───────────┼─────────┼──────
0 │ cat │ Tom │ 7
1 │ dog │ Alfred │ 10
2 │ chameleon │ Linda │ 1
━━━┷━━━━━━━━━━━┷━━━━━━━━━┷━━━━━━
```
2020-07-13 08:39:36 +02:00
The string '\t' can be used to separate on tabs. Note that this is the same as using the from tsv command.
2019-11-19 16:13:10 +01:00
Newlines '\n' are not acceptable separators.
2019-11-08 14:11:04 +01:00
Note that separators are currently provided as strings and need to be wrapped in quotes.
```shell
2020-05-04 23:01:31 +02:00
> open pets.csv --raw | from csv --separator ;
2019-11-08 14:11:04 +01:00
- shell:1:43
2020-05-04 23:01:31 +02:00
1 | open pets.csv --raw | from csv --separator ;
2019-11-08 14:11:04 +01:00
| ^
```
2020-06-23 20:21:47 +02:00
It is also considered an error to use a separator greater than one char:
2019-11-08 14:11:04 +01:00
```shell
2020-05-04 23:01:31 +02:00
> open pets.txt | from csv --separator '123'
2019-11-08 14:11:04 +01:00
error: Expected a single separator char from --separator
- shell:1:37
2020-05-04 23:01:31 +02:00
1 | open pets.txt | from csv --separator '123'
2019-11-08 14:11:04 +01:00
| ^^^^^ requires a single character string input
```