That Blue Square Thing

Computer Science GCSE

Presenting Output as a Table

In Controlled assessment tasks you'll often be asked to present output in a tabular format. This can't really be achieved by using standard print() commands.

In order to use the technique I'm going to outline below your data needs to be stored as a list of lists. Fortunately that's almost certainly the format that it's going to be stored in when it's given to you in the controlled assessment. I'm going to revise some other bits of code here as well - how to read in a list of lists for example.

To complete this activity you need to have a list of lists. The file below should do - I already used this on the read/write files page.

Text file iconSongs text file - save this as a plain text file and rename it as songs.txt. Make sure you know where you saved it!

Note that I know that there are other ways to do this using libraries that you can download from the internets (pretty table and so on). But you can't assume your marker is going to have those libraries installed. You have to be able to do it the hard way!

Reading in the Text File

The strategy here has been dealt with in detail on the read/write files page. Just make sure to save the code in the same folder as your songs.txt file.

# make sure to save this in the same folder as the text file

import csv # import csv module

with open("songs.txt", newline="") as inputfile:
songs = list(csv.reader(inputfile))

Once you have the file read in you should double check that it's actually been read correctly and looks right. To do this it's best to iterate over the list of lists one list at a time.

for i in range(0, len(songs)): # print one line at a time
print(songs[i])

None of that will be new if you've already worked through the read/write files stuff.

An alternative way to dea with the for loop is to use this sort of code:

for row in songs: # print one line at a time
print(row)

That needs slightly less code and, once you have the hang of for loops, is probably easier to do. It's simply a short-cut method of going over each "row" (i.e. each song) in the list of lists one at a time.

Once you know that the list is reading in properly and that everything's there you should comment out the printing lines. You're going to be interested in the tabular print below, not the basic print.

Printing the Data as a Table

This is the clever bit. To an extent you need to not worry about why it works here. If you can use it and figure out how to switch things around to meet whatever requirements you need then that's good enough!

This code will iterate over the list of lists and print it in a more suitable tabular format.

for row in songs:
print("{0:20}{1:^4}{2:20}{3:4}".format(row[0], row[1], row[2],
row[3]))

NB: make sure the list of row[] statements is all on the one line if you copy this.

The print statement is in two parts. The first section - the green bits in curly brackets - deals with the tabular format. The second set of numbers defines the width of each column - so the first column is 20 characters wide.

The row[] statements drag the data in from the list of lists - we're calling each song row, so this is simply referencing the elements of each song (so we need to start from 0 because the first element of a list in Python is numbered 0 of course.

When you run the code you'll note that the column widths aren't right. That's deliberate. You need to figure out for yourself how to switch them around. It's not too tricky...

OK, so at this point I should point out that you're very rarely going to siply have to read a list in and print it out in controlled assessment. You will almost certainly have to do something else to it - add data, remove data, choose data, add up totals and so on. The List of Lists page has some help on how to do that sort of thing. The Lists page might also have some useful things on - perhaps the meu system PDF in particular.

Only printing some columns

It's slightly tricky if you only want to print some of the columns in the table. Until you know how to do it.

Say I only want to print the song title and the year. These are elements 0 and 3 in the song. So all I need to do is:

for row in songs:
print("{0:20}{3:4}".format(row[0], row[1], row[2], row[3]))

Notice that you have to keep all of the row[] statements in place. It won't work otherwise.

Formatting columns

In the code above I threw in a slightly random ^ character (shift + 6) in the second column of my table. This has a meaning - although in that code it won't have been obvious yet.

You can use three simple table formatting techniques:

Try these out with some of the other columns to see how they work.

Adding Column Headers and Lines

You'll probably want to print column headers and lines to define your table. There's more than one way to do this.

The easiest method is probably to add the headers and lines to the list of lists (we're using songs remember). We can then use the simple for loop with the funky curly brackets section to print the whole thing out in one go. This method has particular advantages if you're heavily decomposing your code...

First I'm going to create a new list to print. I'm going to want to headers and lines to this list. I'm using a new list because I don't want to destroy the original data - this way I can still use that data for another job later if I need to.

songsToPrint = songs

Next I'll create a set of lines and add them to the start and end of the list. I'm making each set of lines the same length as my column widths (so if you changed the column widths you'll want to change these as well...).

dashLine = ["-"*20, "-"*4, "-"*20, "-"*4]

songsToPrint.insert(0, dashLine) # adds list at start of the list
songsToPrint.append(dashLine) # adds list at end of the list

Then add the column headers at the start of the list of lists:

headers = ["Song title", "Genre", "Artist", "Year"]

songsToPrint.insert(0, headers) # adds list at start of the list

Now try printing the list again - don't forget that you're now printing songsToPrint rather than songs so the for loop line will need to be changed.

Print an overall header

All I need now is to print the overall header for my table. The simple way to do this is:

print("{0:^48}".format("Songs available"))

Now it's just a case of putting everything together in the right order and you should be able to print a list of lists.

Files

You might want to use:

PDF iconPrinting in a table task sheet - a description of a task similar to one you might get in controlled assessment

PDF iconInstruction sheet - essentially the same code as on this page

Text file iconSongs text file - save this as a plain text file and rename it as songs.txt

Text file iconCode file - a text file with python code in the right order to do all of this. You're advised to work through the code from this page section by section so as you understand it of course. But just in case it won't work, this file should - copy and paste it into IDLE and save the python file in the same location as the songs.txt file

Text file iconDecomposed Code file - an attempt to decompose the code using a general set of functions which could be applied to any program. This is a bit more than just using program specific functions as it aims to create completely reusable blocks of code. Tricky though...