Lesson 1: Displaying file content with qsv table#

We have yet to see what data is in our fruits.csv file. Let’s find out!

Viewing raw file content in the terminal#

If you’re familiar with the Bash terminal on Linux or macOS, you may already know some ways to view raw file contents such as with the cat command. For example you may run:

cat fruits.csv
fruit,price
apple,2.50
banana,3.00
strawberry,1.50

On Windows command prompt (cmd.exe) and Powershell you may use the type command instead. Git Bash and Powershell also have the cat command.

Alternatively with qsv you may run:

qsv select 1- fruits.csv
fruit,price
apple,2.50
banana,3.00
strawberry,1.50

You may learn more about the select command in a later lesson.

Increasing readability with qsv table#

If you look carefully you’ll notice there are various kinds of fruits and their prices as we’ve discovered in the previous lesson using qsv headers. However, you may want to view the data in a “prettier” format. qsv table may assist with this problem by showing aligned output.

qsv table --help
Outputs CSV data as a table with columns in alignment.

This will not work well if the CSV data contains large fields.

Note that formatting a table requires buffering all CSV data into memory.
Therefore, you should use the 'sample' or 'slice' command to trim down large
CSV data before formatting it with this command.

Usage:
    qsv table [options] [<input>]
    qsv table --help

table options:
    -w, --width <arg>      The minimum width of each column.
                           [default: 2]
    -p, --pad <arg>        The minimum number of spaces between each column.
                           [default: 2]
    -a, --align <arg>      How entries should be aligned in a column.
                           Options: "left", "right", "center".
                           [default: left]
    -c, --condense <arg>   Limits the length of each field to the value
                           specified. If the field is UTF-8 encoded, then
                           <arg> refers to the number of code points.
                           Otherwise, it refers to the number of bytes.

Common options:
    -h, --help             Display this message
    -o, --output <file>    Write output to <file> instead of stdout.
    -d, --delimiter <arg>  The field delimiter for reading CSV data.
                           Must be a single character. (default: ,)
    --memcheck             Check if there is enough memory to load the entire
                           CSV into memory using CONSERVATIVE heuristics.

For example, you may run the following command:

qsv table fruits.csv
fruit       price
apple       2.50
banana      3.00
strawberry  1.50

Here we see the content of fruits.csv as a table with aligned columns.

Recap#

In this lesson we’ve covered how to:

  • View raw file content with cat <filepath> (type <filepath> on Windows command prompt), or qsv select 1- <filepath>

  • View CSV file content in a table format with qsv table <filepath>

Now it’s your turn to explore some of the various options qsv table has to offer in Exercise 1.

Exercise 1: Viewing file content with tables#

Binder

Using qsv table and its options, complete each of the following tasks on the fruits.csv and fruits_extended.csv files:

  1. Print fruits.csv as a table with right-aligned column entries.

  2. Print fruits_extended.csv as a table.

  3. Print fruits_extended.csv as a table with each column having a minimum width of 20.

  4. Print fruits_extended.csv as a table with the minimum number of spaces between each column being 20.

  5. Print fruits_extended.csv as a table with each column having a limited length of 5.

Here we show the usage text of qsv table for your reference. Solve this exercise using Thebe, Binder or locally.

qsv table --help
Outputs CSV data as a table with columns in alignment.

This will not work well if the CSV data contains large fields.

Note that formatting a table requires buffering all CSV data into memory.
Therefore, you should use the 'sample' or 'slice' command to trim down large
CSV data before formatting it with this command.

Usage:
    qsv table [options] [<input>]
    qsv table --help

table options:
    -w, --width <arg>      The minimum width of each column.
                           [default: 2]
    -p, --pad <arg>        The minimum number of spaces between each column.
                           [default: 2]
    -a, --align <arg>      How entries should be aligned in a column.
                           Options: "left", "right", "center".
                           [default: left]
    -c, --condense <arg>   Limits the length of each field to the value
                           specified. If the field is UTF-8 encoded, then
                           <arg> refers to the number of code points.
                           Otherwise, it refers to the number of bytes.

Common options:
    -h, --help             Display this message
    -o, --output <file>    Write output to <file> instead of stdout.
    -d, --delimiter <arg>  The field delimiter for reading CSV data.
                           Must be a single character. (default: ,)
    --memcheck             Check if there is enough memory to load the entire
                           CSV into memory using CONSERVATIVE heuristics.