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.
Other ways to view CSV content in the terminal (optional)
Some other ways you may view CSV content for fruits.csv
include running:
qsv cat rows fruits.csv
qsv slice fruits.csv
qsv fmt fruits.csv
If you have the
qsv sqlp
command available, you may runqsv sqlp fruits.csv 'SELECT * FROM fruits' --float-precision 2
. This may also print the shape of the file content(3, 2)
representing 3 rows and 2 columns.
These should all give the same output as qsv select 1- fruits.csv
. However the output may differ if you modify the commands (e.g., add other options) or use a different file format (e.g., fruits.tsv
with tab separated values may be represented with commas as the delimiter instead).
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), orqsv 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#
Using qsv table
and its options, complete each of the following tasks on the fruits.csv
and fruits_extended.csv
files:
Print
fruits.csv
as a table with right-aligned column entries.Print
fruits_extended.csv
as a table.Print
fruits_extended.csv
as a table with each column having a minimum width of20
.Print
fruits_extended.csv
as a table with the minimum number of spaces between each column being20
.Print
fruits_extended.csv
as a table with each column having a limited length of5
.
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.
Solution for task 1
The --align
(or -a
) option may be used to set how entries should be aligned in a column by also passing an argument <arg>
of either left
, center
, or right
. By default the entries are left-aligned.
To right-align all column entries, run:
qsv table fruits.csv --align right
fruit price
apple 2.50
banana 3.00
strawberry 1.50
Solution for task 2
This task is straightforward to complete and primarily for you to view the newly introduced file fruits_extended.csv
. Run:
qsv table fruits_extended.csv
fruit price size availability
apple 2.50 medium available
banana 3.00 medium available
strawberry 1.50 small available
orange 2.00 medium out of stock
pineapple 3.50 large available
grape 4.00 small out of stock
mango 1.80 medium available
watermelon 6.00 large available
pear 2.20 medium out of stock
Solution for task 3
The --width
(or -w
) option may be used to specify the minimum width of each column. By default the width is set to 2
. Run:
qsv table fruits_extended.csv --width 20
fruit price size availability
apple 2.50 medium available
banana 3.00 medium available
strawberry 1.50 small available
orange 2.00 medium out of stock
pineapple 3.50 large available
grape 4.00 small out of stock
mango 1.80 medium available
watermelon 6.00 large available
pear 2.20 medium out of stock
Solution for task 4
The --pad
(or -p
) option may be used to specify the number of spaces between each column. By default --pad
is set to 2
. Run:
qsv table fruits_extended.csv --pad 20
fruit price size availability
apple 2.50 medium available
banana 3.00 medium available
strawberry 1.50 small available
orange 2.00 medium out of stock
pineapple 3.50 large available
grape 4.00 small out of stock
mango 1.80 medium available
watermelon 6.00 large available
pear 2.20 medium out of stock
Completing the previous task 3 and this task 4 may help you understand the visual difference between using --width
and --pad
. Recall you may also use multiple options such as --width
and --pad
together.
Solution for task 5
The --condense
(or -c
) option may be used to limit the length of each column (see qsv table --help
for more details on how this works). Run:
qsv table fruits_extended.csv --condense 5
fruit price size avail...
apple 2.50 mediu... avail...
banan... 3.00 mediu... avail...
straw... 1.50 small avail...
orang... 2.00 mediu... out o...
pinea... 3.50 large avail...
grape 4.00 small out o...
mango 1.80 mediu... avail...
water... 6.00 large avail...
pear 2.20 mediu... out o...