That Blue Square Thing

AQA Computer Science GCSE

July 2019: the AQA CompSci area is almost complete. Most of the content for units 3-6 is now up and there's quite a lot of Unit 7 material. There's still some work to do, particularly on Paper 1 units, but it's getting there.

Programming Concepts - File Handling with Lists

The read/write programs methods described on the File read/write and Advanced read/write pages cope perfectly well when you want to read in a simple text file. But they aren't great when you want to create an array (or list) of items.

You might want to create an array of data from your text file for a number of reasons. It's often an easier way to work through a list of items and use the data associated with them.

Download and save a copy of the countries text file to work through these exercises. Remember, the text file needs to be saved in the same folder as your program.

Text file iconCountries text file - right click and Save As

You need to make sure you save the highscores.txt file in the same folder as your Python files. This is crucial. If the files are saved somewhere else then the programs won't work!

Using Readline

The basic read/write techniques create a set of text which will print over multiple lines. It looks great when you print it - but it's difficult to do very much with it other than add another line at the bottom.

To do things with the items inside the text file it's easiest to read the data in line by line. That's where readline() comes in.

# readfile program using readline
countryFile = open("countries.txt", "r")
aCountry = countryFile.readline() # read in one line
countryFile.close()
print(aCountry)

This will read one country only, which isn't all that useful. To read in multiple countries we can use readlines() instead.

# readfile program using readlines
# this reads in all items and creates a list
countryFile = open("countries.txt", "r")
countryList = countryFile.readlines() # read in all lines
countryFile.close()
print(countryList)

This will create a list of items, but there's a problem - each item has \n at the end of it. Which sort of spoils the way it looks:

['United Kingdom\n', 'France\n', 'Belgium\n', 'United States\n', 'Canada\n', 'Italy\n', 'Spain\n', 'Netherlands\n', 'Germany\n', 'Denmark\n', 'Sweden\n', 'Norway\n', 'Switzerland\n', 'Vatican City\n', 'Monaco']

The \n creates a new line in the text file, so when you use the simple read/write methods and print the data you've read in the list looks nice. The problem is that the \n gets added to the end of each item if you add it to a list - it's a "non-printable character" but the information (when to create a new line) still needs to be included.

To use the list of countries properly, however, we really want to remove the \n.

Removing the Line Breaks

To remove the line breaks (the \n) we'd like to be able to work through the list one item at a time and get rid of the \n. That's not quite as easy as replacing each item in the list, so it's easier to go back one stage at start again using readline.

The key idea here is that it's best to create a new list first to add each item to as you go through.

# 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)

If you want to read in the list of country names as a set of individual lists (and there are good reasons why you might want to...), there's an easy way to adapt this code shown on the Read/Write Lists page in the Programming Project unit.