Loading data

    One of Nu’s most powerful assets in working with data is the open command. It is a multi-tool that can work with a number of different data formats. To see what this means, let’s try opening a json file:

    In a similar way to , opening a file type that Nu understands will give us back something that is more than just text (or a stream of bytes). Here we open a “package.json” file from a JavaScript project. Nu can recognize the JSON text and parse it to a table of data.

    If we wanted to check the version of the project we were looking at, we can use the get command.

    1. 1.0.0

    Nu currently supports the following formats for loading data directly into tables:

    • csv
    • eml
    • ics
    • ini
    • json
    • nuon
    • ods
    • ssv
    • toml
    • tsv
    • url
    • vcf
    • xlsx / xls
    • xml
    • yaml / yml

    But what happens if you load a text file that isn’t one of these? Let’s try it:

    1. > open README.md

    We’re shown the contents of the file.

    An important part of working with data coming from outside Nu is that it’s not always in a format that Nu understands. Often this data is given to us as a string.

    Let’s imagine that we’re given this data file:

    1. > open people.txt
    2. Octavia | Butler | Writer
    3. Antonio | Vivaldi | Composer

    Each bit of data we want is separated by the pipe (‘|’) symbol, and each person is on a separate line. Nu doesn’t have a pipe-delimited file format by default, so we’ll have to parse this ourselves.

    The first thing we want to do when bringing in the file is to work with it a line at a time:

    We can see that we’re working with the lines because we’re back into a table. Our next step is to see if we can split up the rows into something a little more useful. For that, we’ll use the command. split, as the name implies, gives us a way to split a delimited string. We will use ‘s column subcommand to split the contents across multiple columns. We tell it what the delimiter is, and it does the rest:

    1. > open people.txt | lines | split column "|"
    2. ───┬──────────┬───────────┬───────────
    3. # │ column1 │ column2 │ column3
    4. ───┼──────────┼───────────┼───────────
    5. 0 Octavia Butler Writer
    6. 1 Bob Ross Painter
    7. 2 Antonio Vivaldi Composer
    8. ───┴──────────┴───────────┴───────────

    That almost looks correct. It looks like there’s an extra space there. Let’s trim that extra space:

    1. > open people.txt | lines | split column "|" | str trim
    2. ───┬─────────┬─────────┬──────────
    3. # │ column1 │ column2 │ column3
    4. 0 Octavia Butler Writer
    5. 1 Bob Ross Painter
    6. 2 Antonio Vivaldi Composer
    7. ───┴─────────┴─────────┴──────────
    1. > open people.txt | lines | split column "|" | str trim | get column1
    2. ───┬─────────
    3. 0 Octavia
    4. 1 Bob
    5. 2 Antonio
    6. ───┴─────────

    We can also name our columns instead of using the default names:

    Now that our data is in a table, we can use all the commands we’ve used on tables before:

    1. > open people.txt | lines | split column "|" first_name last_name job | str trim | sort-by first_name
    2. # │ first_name │ last_name │ job
    3. ───┼────────────┼───────────┼──────────
    4. 0 Antonio Vivaldi Composer
    5. 1 Bob Ross Painter
    6. 2 Octavia Butler Writer
    7. ───┴────────────┴───────────┴──────────

    There are other commands you can use to work with strings:

    There is also a set of helper commands we can call if we know the data has a structure that Nu should be able to understand. For example, let’s open a Rust lock file:

    1. > open Cargo.lock
    2. # This file is automatically @generated by Cargo.
    3. # It is not intended for manual editing.
    4. [[package]]
    5. name = "adhoc_derive"
    6. version = "0.1.2"

    The “Cargo.lock” file is actually a .toml file, but the file extension isn’t .toml. That’s okay, we can use the from command using the toml subcommand:

    1. > open Cargo.lock | from toml
    2. ──────────┬───────────────────
    3. metadata [row 107 columns]
    4. package [table 130 rows]
    5. ──────────┴───────────────────

    The from command can be used for each of the structured data text formats that Nu can open and understand by passing it the supported format as a subcommand.

    In addition to loading files from your filesystem, you can also load URLs by using the command. This will fetch the contents of the URL from the internet and return it:

    1. > fetch https://blog.rust-lang.org/feed.xml
    2. ──────┬───────────────────
    3. feed {record 2 fields}