Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lilian Blot TPOP Practical Assignment Optional, but you are strongly encourage to do it, especially if you are new to programming Available on Module webpage.

Similar presentations


Presentation on theme: "Lilian Blot TPOP Practical Assignment Optional, but you are strongly encourage to do it, especially if you are new to programming Available on Module webpage."— Presentation transcript:

1 Lilian Blot TPOP Practical Assignment Optional, but you are strongly encourage to do it, especially if you are new to programming Available on Module webpage from next week (week 6) Deadline: Friday 14 November at 12:00 pm.  submitted by to  with the subject: "TPOP Assignment“ Feedback and Mark given by appointment ONLY must improve / pass / good (2.1) / very good (1st) Autumn 2014 TPOP 1

2 Lilian Blot PERSISTENT DATA USING TEXT FILES Lecture 10 Autumn 2014 TPOP 2

3 Lilian Blot Review To date we have use variables to store set of data: String for collection of characters (immutable)  ‘this is a sentence’ Tuples for collection of objects (immutable)  (1, ‘another sentence’, 5.3, (3,0), ‘A’) Lists for collection of objects (mutable)  [1, ‘TPOP’, 5.3, (3,0), [1,4,2],‘A’] Dictionaries for collection of mapping keys and values (mutable)  {1: ‘one’, 2:‘two’, ‘French’:{1:’un’, 2:’deux’}, ‘digits’:[‘0’,’1’,’2’,’3’,’4’,’5’,’6’,’7’,’8’,’9’]} Autumn 2014 TPOP 3

4 Lilian Blot Overview However when the program exits, all data is lost. There is a need for persistent data. Today’s lecture will address:  Opening/closing text files  Reading a text file content  Writing on a text file  Formatting data structure Autumn 2014 TPOP 4

5 Lilian Blot Writing data to a file Code: save the content of a list into a text file. Autumn 2014 TPOP 5 Method nameuseExplanation openf=open(filename,’r’) Open a file called filename for read only. Return a object. See help(file) in Python shell. openf=open(filename,’w’) Open a file called filename for write only. Return a object. closef.close()Close the file. writef.write(aString) Add the string aString at the end of the file. f must be opened (using open in row 2) and not closed (row 3 not used yet).

6 Lilian Blot Reading data from a file Code: read the content of a text file and store it into a list. Autumn 2014 TPOP 6 Method nameuseExplanation read f.read() f.read(n) Reads and returns a string of n characters, or the entire file content as a single string if n is not provided readlinef.readline() Reads and returns the next line of the file with all text up to and including the newline character (‘\n’). readlines f.readlines() f.readlines(n) Reads and returns a list of n strings each representing a single line of the file. If n is not provided, then all lines of the file are returned.

7 Lilian Blot Representing a Table (spreadsheet) How could we represent the following spreadsheet in Python? Autumn 2014 TPOP 7 SurnameFirstnameNationality PaulCairnsUK WillSmithUK LilianBlotIrrevocably French ……… table = [[’paul’,’Cairns’,’UK’], [’will’,’Smith’,’UK’], …] Code 2D list

8 Lilian Blot Formatting Strings We may want to format the values into a string before writing to a file: Autumn 2014 TPOP 8 CharacterDescription d, iInteger or long integer fFloating point as m.ddddd cSingle character s String or any Python object that can be converted to a string by using the str function. >>> aString = '%s is celebrating is %ith birthday next year‘ %('lilian',29) >>> aString 'lilian is celebrating is 29th birthday next year' >>> Code

9 Lilian Blot Formatting Strings We may want to format the values into a string before writing to a file: Autumn 2014 TPOP 9 ModifierExampleDescription number%20dPut the value in a field of 20 characters wide. -%-20dSame as above but left-justified +%+20dSame as above but right-justified 0%020d Put the value in a field of 20 characters wide, fill in with leading zeros..%20.2f Put the value in a field of 20 characters wide, with two characters to the right of the decimal point.

10 Lilian Blot Representing a Table (spreadsheet) Rewriting the table with string formatting Autumn 2014 TPOP 10 SurnameFirstnameNationality PaulCairnsUK WillSmithUK LilianBlotIrrevocably French ……… table = [[’paul’,’Cairns’,’UK’], [’will’,’Smith’,’UK’], …] Code

11 Lilian Blot Representing a Table (spreadsheet) What about missing data? Autumn 2014 TPOP 11 SurnameFirstnamemiddlenameNationality PaulCairnsUK WillSmithUK LilianBlotJ-LIrrevocably French AlanFrischM ………… table = [[’paul’,’Cairns’,’UK’], [’will’,’Smith’,’UK’], …] Code Is this working?

12 Lilian Blot Defining a file Format CSV files: each field is separated by a comma. Missing fields: Autumn 2014 TPOP 12 Paul, Cairns, UK Will, Smith, UK Lilian, blot, French File Paul, Cairns,, UK Will, Smith,, UK Lilian, blot, J-L, French Alan, Frisch, M, File

13 Lilian Blot Defining a file Format Extensible Markup Language (XML) Autumn 2014 TPOP 13 "Aalborg, Denmark ",AAL "Aalesund, Norway ",AES "Aarhus, Denmark - Bus service ",ZID CSV File Aalborg, Denmark AAL Aalesund, Norway AES XML File

14 Lilian Blot Summary We have seen the need of persistent data Reading/writing text file Defining a file format Formatting string to a specified format Another type of files: Binary Files (not this year) Autumn 2014 TPOP 14

15 Lilian Blot Exercises Get the file containing the average temperatures in the UK at the following address:  nked/UK.txt nked/UK.txt  As you can see, one line contains many different years  Reformat the file so the rows are years in increasing order, and columns are month from January to December. This is a difficult exercise, so do it once you finished the exercises provided in the practical. Autumn 2014 TPOP 15


Download ppt "Lilian Blot TPOP Practical Assignment Optional, but you are strongly encourage to do it, especially if you are new to programming Available on Module webpage."

Similar presentations


Ads by Google