Python > Assignment #7: Tables and CSV Files
Deadline: Wed 16. 11. 2016, 07:47 CET
Note: Have a look at the exercises from the last lecture.
Note: You may also want to consult the official python tutorial.
Assignment: Create a module table
able to load, save and pretty-print simple .csv
files. Write a program that, using this module, loads a file, removes the first column of the data, sorts the entries by the then second field, pretty-prints and finally saves the output.
Example
For a simple input file:
ID | Given name | Family name | Age |
---|---|---|---|
1 | John | Doe | 33 |
2 | David | Tyler | 24 |
3 | Little | One | 5 |
4 | Granny | Smith | 148 |
5 | Golden | Delicious | 111 |
The correct output will be:
Given name | Family name | Age |
---|---|---|
Golden | Delicious | 111 |
John | Doe | 33 |
Little | One | 5 |
Granny | Smith | 148 |
David | Tyler | 24 |
Code organization
Create two files: the first file – table.py
– will serve as a library of CSV-related functions;
the second – main.py
– will be the main program that can be run.
The table.py
module should contain three functions (and helper functions, if you want):
load(filename)
– loads CSV-formatted data from an input file; the first line of the input file contains column headers. Return a(headers, data)
tuple. [1pts]
save(filename, headers, data)
– saves the provided data to a file in CSV format [1pts]
show(headers, data)
– prints the table in a readable format. Every field is padded with spaces, so that the columns in the output line up; grid lines are drawn. [2pts]+------------+-------------+-----+ | Given name | Family name | Age | +------------+-------------+-----+ | Golden | Delicious | 111 | | John | Doe | 33 | | Little | One | 5 | | Granny | Smith | 148 | | David | Tyler | 24 | +------------+-------------+-----+
You may want to use something built-in for padding, e.g.:
The main.py
might look like this: [1pts]
import table
headers, data = table.load('input.csv')
# TODO: remove the first column
# TODO: sort by the (now) second column
table.show(headers, data)
table.save('output.csv', headers, data)
(The import statement can import standard libraries as well as your own modules,
provided that both the main
program and the table
module are in the same directory.)
CSV Format
CSV format – comma-separated values – is a very simple data-exchange format for encoding tabular data. Each row of the table is represented by one line in the CSV file, containing the row’s fields’ values separated by, you guessed it, commas.
(Troubles only arise when the fields are allowed to contain commas and/or linebreaks; various mutually-incompatible quoting and escaping styles exist and are widely used.)
Python has built-in support for reading and writing CSV files in its csv
module. While you should usually use it, for this assignment, write your own code using only file
methods.
Bonuses
Improve the pretty-printing functionality of table.show(…)
:
use as little padding as possible: pad every column to the length of its longest entry [1pts]
produce a fancier output with table-drawing characters. Some inspiration: [1pts]
┌────────────┬─────────────┬─────┐
│ Given name │ Family name │ Age │
├────────────┼─────────────┼─────┤
│ Golden │ Delicious │ 111 │
│ John │ Doe │ 33 │
│ Little │ One │ 5 │
│ Granny │ Smith │ 148 │
│ David │ Tyler │ 24 │
└────────────┴─────────────┴─────┘
╔════════════╦═════════════╦═════╗
║ Given name ║ Family name ║ Age ║
╟────────────╫─────────────╫─────╢
║ Golden ║ Delicious ║ 111 ║
║ John ║ Doe ║ 33 ║
║ Little ║ One ║ 5 ║
║ Granny ║ Smith ║ 148 ║
║ David ║ Tyler ║ 24 ║
╚════════════╩═════════════╩═════╝
╔════════════╦═════════════╦═════╗
║ Given name ║ Family name ║ Age ║
╠════════════╬═════════════╬═════╣
║ Golden ║ Delicious ║ 111 ║
║ John ║ Doe ║ 33 ║
║ Little ║ One ║ 5 ║
║ Granny ║ Smith ║ 148 ║
║ David ║ Tyler ║ 24 ║
╚════════════╩═════════════╩═════╝
╔────────────╦─────────────╦─────╗
│ Given name │ Family name │ Age │
╠────────────╬─────────────╬─────╣
│ Golden │ Delicious │ 111 │
│ John │ Doe │ 33 │
│ Little │ One │ 5 │
│ Granny │ Smith │ 148 │
│ David │ Tyler │ 24 │
╚────────────╩─────────────╩─────╝
(Note that all the styles use 11 distinct characters.)