Y10 05 CT26 Slides
Y10 05 CT26 Slides
Learning objectives
In this lesson you will learn how to:
For more detail on this topic, and additional learner activities, refer to
Topic 1.11 of the student book.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-05-CT26: Reading files
You used lists and arrays to deal with data in one dimension.
So far, you have been hard coding this data, or asking the user to
enter it directly into the program.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-05-CT26: Reading files
The problem…
Hard-coded data exists only within the code, and only when the
program is running.
In other words, the data is only stored in memory while the program
is running. It is not persistent.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-05-CT26: Reading files
Structured data
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-05-CT26: Reading files
Structured data
These represent
column headings
and are optional.
This is a
single
record.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-05-CT26: Reading files
There are different ways of reading the data in from a file. The
method used will depend on the type of file and the structure of the
data inside it.
For the examples you will be working with, when you read data in
from a file, you will use lists – first one dimensional and then two
dimensional.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-05-CT26: Reading files
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-05-CT26: Reading files
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-05-CT26: Reading files
The result
['ID,Username,MonthJoined,BirthMonth\n’,
'1,Alice,2,4\n','2,Bob,3,2\n’, '3,Charles,9,4\n’,
'4,Dinah,10,12\n’, '5,Edwardo,1,11\n’,
'6,Fran,8,3\n’, '7,Georgia,9,9\n']
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-05-CT26: Reading files
When you look at a file in an editor, you can see the separate lines
clearly, but how are they represented in a computer?
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-05-CT26: Reading files
Representing a line
The \n is the ‘new line’ escape character. It is hidden from us, but it is
what the computer uses to recognise the end of a line.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-05-CT26: Reading files
Non-printable characters
Carriage CR Pushes the location for the next output back ‘\r’ 0x0D
Return to the far left.
Line Feed / LF Rolls the location for the next output down ‘\n’ 0x0A
New Line to the next line.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-05-CT26: Reading files
String manipulation
You will need to use some of these functions to help you work with
the text in files.
In this instance, you need to remove the new line escape character
from each line as it is being inserted into the list.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-05-CT26: Reading files
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.