cosc 1306—computer science and programming python functions

95
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS Jehan-François Pâris [email protected]

Upload: opal

Post on 17-Jan-2016

54 views

Category:

Documents


0 download

DESCRIPTION

COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS. Jehan-François Pâris [email protected]. Module Overview. We will learn how to read, create and modify files Pay special attention to pickled files They are very easy to use!. The file system. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

COSC 1306—COMPUTER SCIENCE AND PROGRAMMING

PYTHON FUNCTIONS

Jehan-François Pâ[email protected]

Page 2: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Module Overview

• We will learn how to read, create and modify files– Pay special attention to pickled files

• They are very easy to use!

Page 3: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

The file system

• Provides long term storage of information. • Will store data in stable storage (disk)• Cannot be RAM because:

– Dynamic RAM loses its contents when powered off

– Static RAM is too expensive– System crashes can corrupt contents of the main

memory

Page 4: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Overall organization

• Data managed by the file system are grouped in user-defined data sets called files

• The file system must provide a mechanism for naming these data– Each file system has its own set of conventions– All modern operating systems use a

hierarchical directory structure

Page 5: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Windows solution

• Each device and each disk partition is identified by a letter– A: and B: were used by the floppy drives– C: is the first disk partition of the hard drive– If hard drive has no other disk partition,

D: denotes the DVD drive• Each device and each disk partition has its own

hierarchy of folders

Page 6: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Windows solution

C:

WindowsUsers

Second diskD:

Program Files

Flash driveF:

Page 7: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

UNIX/LINUX organization

• Each device and disk partition has its own directory tree– Disk partitions are glued together through the

operation to form a single tree• Typical user does not know where her files

are stored

Page 8: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

UNIX/LINUX organizationRoot partition

bin

usr

/ Other partition

The magicmount

Second partition can be accessed as /usr

Page 9: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Mac OS organization

• Similar to Windows – Disk partitions are not merged – Represented by separate icons on the

desktop

Page 10: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Accessing a file (I)

• Your Python programs are stored in a folder AKA directory– On my home PC it is

C:\Users\Jehan-Francois Paris\Documents\Courses\1306\Python

• All files in that directory can be directly accessed through their names– "myfile.txt"

Page 11: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Accessing a file (II)

• Files in subdirectories can be accessed by specifying first the subdirectory– Windows style:

• "test\\sample.txt" – Note the double backslash

– Linux/Unix/Mac OS X style:• "test/sample.txt"

– Generally works for Windows

Page 12: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Why the double backslash?

• The backslash is an escape character in Python– Combines with its successor to represent

non-printable characters• ‘\n’ represents a newline• ‘\t’ represents a tab

– Must use ‘\\’ to represent a plain backslash

Page 13: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Accessing a file (III)

• For other files, must use full pathname– Windows Style:

• "C:\\Users\\Jehan-Francois Paris\\Documents\\Courses\\1306\\Python\\myfile.txt"

Page 14: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Accessing file contents

• Two step process:– First we open the file– Then we access its contents

• Read• Write

• When we are done, we close the file.

Page 15: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

What happens at open() time?

• The system verifies– That you are an authorized user– That you have the right permission

• Read permission• Write permission• Execute permission exists but doesn’t apply

and returns a file handle /file descriptor

Page 16: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

The file handle

• Gives the user– Direct access to the file

• No directory lookups– Authority to execute the file operations whose

permissions have been requested

Page 17: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Python open()

• open(name, mode = ‘r’, buffering = -1)

where– name is name of file– mode is permission requested

• Default is ‘r’ for read only– buffering specifies the buffer size

• Use system default value (code -1)

Page 18: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

The modes

• Can request– ‘r’ for read-only– ‘w’ for write-only

• Always overwrites the file– ‘a’ for append

• Writes at the end– ‘r+’ or ‘a+’ for updating (read + write/append)

Page 19: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Examples

• f1 = open("myfile.txt") same asf1 = open("myfile.txt", "r")

• f2 = open("test\\sample.txt", "r")

• f3 = open("test/sample.txt", "r")

• f4 = open("C:\\Users\\Jehan-Francois Paris\\Documents\\Courses\\1306\\Python\\myfile.txt")

Page 20: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Reading a file

• Three ways:– Global reads– Line by line– Pickled files

Page 21: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Global reads

• fh.read()– Returns whole contents of file specified by

file handle fh– File contents are stored in a single string

that might be very large

Page 22: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Example

• f2 = open("test\\sample.txt", "r") bigstring = f2.read()print(bigstring)f2.close() # not required

Page 23: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Output of example

• To be or not to be that is the questionNow is the winter of our discontent

– Exact contents of file ‘test\sample.txt’

Page 24: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Line-by-line reads

• for line in fh : # do not forget the column #anything you wantfh.close() # not required

Page 25: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Example

• f3 = open("test/sample.txt", "r") for line in f3 : # do not forget the column

print(line)f3.close() # not required

Page 26: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Output

• To be or not to be that is the question

Now is the winter of our discontent

– With one or more extra blank lines

Page 27: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Why?

• Each line ends with an end-of-line marker• print(…) adds an extra end-of-line

Page 28: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Trying to remove blank lines

• print('----------------------------------------------------')f5 = open("test/sample.txt", "r") for line in f5 : # do not forget the column print(line[:-1]) # remove last charf5.close() # not requiredprint('-----------------------------------------------------')

Page 29: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

The output

• ----------------------------------------------------To be or not to be that is the questionNow is the winter of our disconten-----------------------------------------------------

• The last line did not end with an EOL!

Page 30: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

A smarter solution (I)

• Only remove the last character if it is an EOL– if line[-1] == ‘\n’ :

print(line[:-1]else print line

Page 31: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

A smarter solution (II)

• print('----------------------------------------------------')fh = open("test/sample.txt", "r")for line in fh : # do not forget the column if line[-1] == '\n' : print(line[:-1]) # remove last char else : print(line)print('-----------------------------------------------------')fh.close() # not required

Page 32: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

It works!

• ----------------------------------------------------To be or not to be that is the questionNow is the winter of our discontent-----------------------------------------------------

Page 33: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Making sense of file contents

• Most files contain more than one data item per line– COSC 713-743-3350

UHPD 713-743-3333• Must split lines

– mystring.split(sepchar)where sepchar is a separation character• returns a list of items

Page 34: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Splitting strings

• >>> text = "Four score and seven years ago">>> text.split()['Four', 'score', 'and', 'seven', 'years', 'ago']

• >>>record ="1,'Baker, Andy', 83, 89, 85">>> record.split(',')[' 1', "'Baker", " Andy'", ' 83', ' 89', ' 85']

Not what we wanted!

Page 35: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Example

# how2split.pyprint('----------------------------------------------------')f5 = open("test/sample.txt", "r")for line in f5 :

words = line.split() for xxx in words : print(xxx)f5.close() # not requiredprint('-----------------------------------------------------')

Page 36: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Output

• ----------------------------------------------------Tobe…ofourdiscontent-----------------------------------------------------

Page 37: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Other separators (I)

• Commas– CSV Excel format

• Values are separated by commas• Strings are stored without quotes

–Unless they contain a comma• “Doe, Jane”, freshman, 90, 90

–Quotes within strings are doubled

Page 38: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Other separators (II)

• Tabs( ‘\t’)– Advantages:

• Your fields will appear nicely aligned• Spaces, commas, … are not an issue

– Disadvantage:• You do not see them

–They look like spaces

Page 39: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Why it is important

• When you must pick your file format, you should decide how the data inside the file will be used:– People will read them– Other programs will use them– Will be used by people and machines

Page 40: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

An exercise

• Converting our output to CSV format– Replacing tabs by commas

• Easy–Will use string replace function

Page 41: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

First attempt

• fh_in = open('grades.txt', 'r') # the 'r' is optionalbuffer = fh_in.read()newbuffer = buffer.replace('\t', ',')fh_out = open('grades0.csv', 'w')fh_out.write(newbuffer)fh_in.close()fh_out.close()print('Done!')

Page 42: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

The output

• Alice 90 90 90 90 90Bob 85 85 85 85 85Carol 75 75 75 75 75

becomes• Alice,90,90,90,90,90

Bob,85,85,85,85,85Carol,75,75,75,75,75

Page 43: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Dealing with commas (I)

• Work line by line• For each line

– split input into fields using TAB as separator– store fields into a list

• Alice 90 90 90 90 90becomes[‘Alice’, ’90’, ’90’, ’90’, ’90’, ’90’]

Page 44: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Dealing with commas (II)

– Put within double quotes any entry containing one or more commas

– Output list entries separated by commas• ['"Baker, Alice"', 90, 90, 90, 90, 90]

becomes"Baker, Alice",90,90,90,90,90

Page 45: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Dealing with commas (III)

• Our troubles are not over:– Must store somewhere all lines until we are

done– Store them in a list

Page 46: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Dealing with double quotes

• Before wrapping items with commas with double quotes replace– All double quotes by pairs of double quotes– 'Aguirre, "Lalo" Eduardo'

becomes'Aguirre, ""Lalo"" Eduardo'then'"Aguirre, ""Lalo"" Eduardo"'

Page 47: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

General organization (I)

• linelist = [ ]• for line in file

– itemlist = line.split(…)– linestring = '' # empty string– for each item in itemlist

• remove any trailing newline• double all double quotes• if item contains comma, wrap• add to linestring

Page 48: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

General organization (II)

• for line in filefor line in file– ……– for each item in itemlistfor each item in itemlist

• double all double quotesdouble all double quotes• if item contains comma, wrapif item contains comma, wrap• add to linestringadd to linestring

– append linestring to stringlist

Page 49: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

General organization (III)

• for line in filefor line in file– ……– remove last comma of linestring– add newline at end of linestring– append linestring to stringlist

• for linestring in in stringline – write linestring into output file

Page 50: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

The program (I)

• # betterconvert2csv.py""" Convert tab-separated file to csv"""fh = open('grades.txt','r') #input filelinelist = [ ] # global data structurefor line in fh : # outer loop itemlist = line.split('\t') # print(str(itemlist)) # just for debugging linestring = '' # start afresh

Page 51: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

The program (II)

• for item in itemlist : #inner loop item = item.replace('"','""') # for quotes if item[-1] == '\n' : # remove it item = item[:-1] if ',' in item : # wrap item linestring += '"' + item +'"' + ',' else : # just append linestring += item +',' # end of inside for loop

Page 52: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

The program (III)

• # must replace last comma by newline linestring = linestring[:-1] + '\n' linelist.append(linestring)# end of outside for loopfh.close()fhh = open('great.csv', 'w')for line in linelist : fhh.write(line)fhh.close()

Page 53: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Notes

• Most print statements used for debugging were removed– Space considerations

• Observe that the inner loop adds a comma after each item– Wanted to remove the last one

• Must also add a newline at end of each line

Page 54: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

The input file

• Alice 90 90 90 90 90Bob 85 85 85 85 85Carol 75 75 75 75 75Doe, Jane 90 90 90 80 70Fulano, Eduardo "Lalo" 90 90 9090

Page 55: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

The output file

• Alice,90,90,90,90,90Bob,85,85,85,85,85Carol ,75,75,75,75,75"Doe, Jane",90,90 ,90 ,80 ,75"Fulano, Eduardo ""Lalo""",90,90,90,90

Page 56: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Mistakes being made (I)

• Mixing lists and strings:– Earlier draft of program declared

• linestring = [ ]and did• linestring.append(item)

– Outcome was• ['Alice,', '90,'. … ]

instead of• 'Alice,90, …'

Page 57: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Mistakes being made (II)

• Forgetting to add a newline– Output was a single line

• Doing the append inside the inner loop:– Output was

• Alice,90Alice,90,90Alice,90,90,90…

Page 58: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Mistakes being made

• Forgetting that strings are immutable:– Trying to do

• linestring[-1] = '\n'

instead of• linestring = linestring[:-1] + '\n'

– Bigger issue:• Do we have to remove the last comma?

Page 59: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Could we have done better? (I)

• Make the program more readable by decomposing it into functions– A function to process each line of input

• do_line(line)– Input is a string ending with newline–Output is a string in CSV format–Should call a function processing individual

items

Page 60: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Could we have done better? (II)

– A function to process individual items• do_item(item)

– Input is a string–Returns a string

• With double quotes "doubled"• Without a newline• Within quotes if it contains a comma

Page 61: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

The new program (I)

• def do_item(item) : item = item.replace('"','""') if item[-1] == '\n' : item = item[:-1] if ',' in item : item ='"' + item +'"' return item

Page 62: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

The new program (II)

• def do_line(line) : itemlist = line.split('\t') linestring = '' # start afresh for item in itemlist : linestring += do_item(item) +',' linestring += '\n' return linestring

Page 63: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

The new program (III)

• fh = open('grades.txt','r')linelist = [ ]for line in fh : linelist.append(do_line(line))fh.close()

Page 64: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

The new program (IV)

• fhh = open('great.csv', 'w')for line in linelist : fhh.write(line)fhh.close()

Page 65: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Why it is better

• Program is decomposed into small modules that are much easier to understand– Each fits on a PowerPoint slide

Page 66: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

The break statement

• Makes the program exit the loop it is in• In next example, we are looking for

first instance of a string in a file– Can exit as soon it is found

Page 67: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Example (I)

• searchstring= input('Enter search string:')found = Falsefh = open('grades.txt')for line in fh : if searchstring in line : print(line) found = True break

Page 68: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Example (II)

• if found == True : print("String %s was found" % searchstring)else : print("String %s NOT found " % searchstring)

Page 69: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Flags

• A variable like found– That can either be True or False– That is used in a condition for an if or a while

is often referred to as a flag

Page 70: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

A dumb mistake

• Unlike C and its family of languages,Python does not let you write– if found = True

for– if found == True

• There are still cases where we can do mistakes!

Page 71: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Example

• >>> b = 5>>> c = 8>>> a = b = c>>> a8

• >>> a = b == c>>> aTrue

Page 72: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

HANDLING EXCEPTIONS

Page 73: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

When a wrong value is entered

• When user is prompted for– number = int(input("Enter a number: ")

and enters– a non-numerical string

a ValueError exception is raised and the program terminates

• Python a programs catch errors

Page 74: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

The try… except pair (I)

• try:<statements being tried>

except Exception as ex:<statements catching the exception>

• Observe– the colons– the indentation

Page 75: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

The try… except pair (II)

• try:<statements being tried>

except Exception as ex:<statements catching the exception>

• If an exception occurs while the program executes the statements between the try and the except, control is immediately transferred to the statements after the except

Page 76: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

A better example

• done = Falsewhile not done : filename= input("Enter a file name: ") try : fh = open(filename) done = True except Exception as ex: print ('File %s does not exist' % filename)print(fh.read())

Page 77: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

An Example (I)

• done = Falsewhile not done : try : number = int(input('Enter a number:')) done = True except Exception as ex: print ('You did not enter a number')print ("You entered %.2f." % number)input("Hit enter when done with program.")

Page 78: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

A simpler solution

• done = Falsewhile not done myinput = (input('Enter a number:')) if myinput.isdigit() : number = int(myinput) done = True else : print ('You did not enter a number')print ("You entered %.2f." % number)input("Hit enter when done with program.")

Page 79: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

PICKLED FILES

Page 80: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Pickled files

• import pickle – Provides a way to save complex data

structures in a file– Sometimes said to provide a

serialized representation of Python objects

Page 81: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Basic primitives (I)

• dump(object,fh)– appends a sequential representation of

object into file with file handle fh– object is virtually any Python object– fh is the handle of a file that must have been

opened in 'wb' mode

b is a special option allowing towrite or read binary data

Page 82: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Basic primitives (II)

• target = load( filehandle)– assigns to target next pickled object stored in

file filehandle– target is virtually any Python object– filehandle id filehandle of a file that was

opened in rb mode

Page 83: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Example (I)

• >>> mylist = [ 2, 'Apples', 5, 'Oranges']• >>> mylist

[2, 'Apples', 5, 'Oranges']• >>> fh = open('testfile', 'wb') # b is for BINARY• >>> import pickle• >>> pickle.dump(mylist, fh)• >>> fh.close()

Page 84: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Example (II)

• >>> fhh = open('testfile', 'rb') # b is for BINARY• >>> theirlist = pickle.load(fhh)• >>> theirlist

[2, 'Apples', 5, 'Oranges']• >>> theirlist == mylist

True

Page 85: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

What was stored in testfile?

• Some binary data containing the strings 'Apples' and 'Oranges'

Page 86: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Using ASCII format

• Can require a pickled representation of objects that only contains printable characters– Must specify protocol = 0

• Advantage:– Easier to debug

• Disadvantage:– Takes more space

Page 87: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Example

• import picklemydict = {'Alice': 22, 'Bob' : 27}fh = open('asciifile.txt', 'wb') # MUST be 'wb'pickle.dump(mydict, fh, protocol = 0)fh.close()fhh = open('asciifile.txt', 'rb')theirdict = pickle.load(fhh)print(mydict)print(theirdict)

Page 88: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

The output

• {'Bob': 27, 'Alice': 22}{'Bob': 27, 'Alice': 22}

Page 89: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

What is inside asciifile.txt?

• (dp0VBobp1L27LsVAlicep2L22Ls.

Page 90: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Dumping multiple objects (I)

• import picklefh = open('asciifile.txt', 'wb')for k in range(3, 6) : mylist = [i for i in range(1,k)] print(mylist) pickle.dump(mylist, fh, protocol = 0)fh.close()

Page 91: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Dumping multiple objects (II)

• fhh = open('asciifile.txt', 'rb')lists = [ ] # initializing list of listswhile 1 : # means forever try:

lists.append(pickle.load(fhh))except EOFError :

breakfhh.close()print(lists)

Page 92: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Dumping multiple objects (III)

• Note the way we test for end-of-file (EOF)

– while 1 : # means forever try:

lists.append(pickle.load(fhh)) except EOFError :

break

Page 93: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

The output

• [1, 2][1, 2, 3][1, 2, 3, 4][[1, 2], [1, 2, 3], [1, 2, 3, 4]]

Page 94: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

What is inside asciifile.txt?

• (lp0L1LaL2La.(lp0L1LaL2LaL3La.(lp0L1LaL2LaL3LaL4La.

Page 95: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS

Practical considerations

• You rarely pick the format of your input files– May have to do format conversion

• You often have to use specific formats for you output files– Often dictated by program that will use them

• Otherwise stick with pickled files!