That Blue Square Thing

AQA Computer Science GCSE

February 2019: this area of the site is being built just now. There are areas where there is no content yet. That will get added over the next 6 months or so.

Programming project - Read/Write List Files

The basics of file handling (reading and writing text files in order to save data) is dealt with in Unit 2 - Read/Write files. This page takes the techniques learned there a but further and reads in text files as lists.

Having data files as lists means that it's much easier to add items or remove items from them. For example, a high scores list would be easy to add an item to or remove an item and could even be sorted.

Don't forget that a Python list is an array. An exam will always refer to them as arrays.

Method 1 - Using Readline

This uses the same method as on the Unit 2 - Using readline for lists page with just a little addition.

Text file iconCountries text file - right click and Save As

All you need to do is add in two square brackets on the line where the rstrip command is used:

country = [country.rstrip("\r\n")] # remove the \n
countryList.append(country) # add the country to the list

The square brackets change the variable country from a string to an array with just 1 element - the same string. This doesn't look much but is a fundamental change to the type of data.

Here's all the code you need for this:

# read in a list and remove \n
countryList = [] # set up an empty list

countryFile = open("countries.txt", "r")
for country in countryFile: # work through line by line
country = [country.rstrip("\r\n")] # remove the \n
countryList.append(country) # add the country to the list

countryFile.close()
print(countryList)

It is possible to do this in other ways, but much quicker to do it when you read in the data first of all.

Method 2 - Using CSV Reader

The text file to use needs to be on lines with each item on a line separated by commas - see the text files below. This makes is a CSV file type (comma separated values), which is easy to make into a list in Python.

Text file iconSongs text file - save this as a plain text file called readinfile.txt. The file needs to be saved in the same place as the code. Save it as countrydata.txt.

A version of the countries text file is provided with data added to make some programs I might teach quicker to test. It's below.

Text file iconCountries with data text file - save this as a plain text file. The file needs to be saved in the same place as the code.

The strategy here is straight forward. If you open the text file you'll see that it's organised in rows. That means we can:

  1. create an empty list to store the contents of the text file in (I'll call it songList)
  2. read in the first line of text and store it as a list
  3. add this list to songList
  4. keep reading in new lines of text, storing them as lists and adding them to songList
  5. when we get to the end of the text file stop

It is possible to code that process line by line, but there's a quicker way using a module called csv. This won't work with every kind of text file you might want to use, but is a good place to start.

Here's some code.

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

import csv # import csv module

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

print(songs) # print list all in one go
print()

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

That's a nice simple way of reading in a file in about three lines of code. Not always what you want to do but a good starting point!.

Using Text Data

So, there's a list of songs. Say I want just the songs which are Dance tunes (ones with DA next to them), how do I do that?

Here's a strategy:

  1. read in the whole list into a list of lists - as above
  2. set up an empty list called danceSongs to store the DA tunes on
  3. use a for loop to look at each song in turn - passing each song into a temporary variable called aSong
  4. look at the second element in currentSong (index 1 remember)
  5. if this element is "DA" then add the song to the list of Dance tracks
  6. otherwise do nothing

Let's see how this would work:

# code to pick out just dance tracks

import csv # import csv module

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

danceSongs = [ ]

for aSong in songs: # look at one line at a time
if aSong[1] == "DA": # look at current song's genre
danceSongs.append(aSong) # add to the list

for tune in danceSongs: # print one line at a time
print(tune)

Once you can do this you can look through lists of lists without too much trouble. The ley is to think about writing to a new list each time you want to find something specific. You can then iterate over that list to pull out the data you actually need.