Before Reading Week Lists List methods Nested Lists Looping through lists using for loops While loops
This Week and Maybe Next Special Characters Review Files What are they? Opening and closing files Reading and writing to files Comma Separated Value Files
Strings Using Conversion Specifiers We some'mes would like to insert values of variables into strings: A1 = 60 A2 = 75 A3 = 88 We would like: The average of 60, 75 and 88 is 74.33. How do we print this with our variables? >>>print( The average of, A1,,, A2, and, A3, is, (A1+A2+A3)/3) Does this work?
Strings Using Conversion Specifiers We displayed: The average of 60, 75 and 88 is 74.33333333333333. Q. What s wrong? A. Spacing is wrong around commas and periods. We have many more decimal places than wanted. Q. How can we fix it? A. Use conversion specifiers. >>>print( The average of %d, %d and %d is %.2f %(A1, A2, A3, (A1+A2+A3)/3.0)) The average of 60, 75 and 88 is 74.33.
Common Conversion Specifiers %d display the object as a decimal integer %f display the object as a floating point with 6 decimal places %.2f display the object as a floating point with 2 decimal places %s display the object as a string Q. What else do we use % for? A. Modulus. We say that % is overloaded.
Invisible Characters How do we indicate a tab, newline or or inside a string? \t tab \ double quote \n new line \ single quote \\ backslash These are called escape sequences. print( This string has a \n newline and a \t tab. This string has a newline and a tab.
Files Files what are they? Can think of as a collection of stored information Computer thinks as a sequence of bits possibly arranged in characters Files have names: my_python.py, homework.txt, etc. What can we do with a file? Read from a file and write to a file
Opening Files How do we open a file in Python? myfile = open( story.txt, r ) Open is the Python function Story.txt is the name of the file to be opened myfile is a variable that is assigned the file object (also called stream or reader). r is a string indicating what we will do with the file (read, write, append)
Using Files After we are finished with a file we must close it: myfile.close() When we write to a file, we have two choices: Write: myfile = open( filename, w ) Append: myfile = open( filename, a ) write replaces the file filename append appends to the file filename
Reading Files There are many ways to read from a file. 1. Using a for loop: myfile = open( filename, r ) for line in myfile: <do something with the line> 2. Read the whole file at once into a list of strings: myfile = open( filename, r ) list_of_lines = myfile.readlines()
Reading Files 3. Read the entire file into a string: myfile = open( filename, r ) s = myfile.read() 4. Read a specific number of characters: myfile = open( filename, r ) s = myfile.read(10) reads 10 characters s = myfile.read(10) reads then next 10 characters
Reading Files 5. Read one line at a time: myfile = open( filename, r ) line = myfile.readline() reads a line line = myfile.readline() reads the next line
End Of File (EOF) How do we know when we reach the end of the file? A for loop automatically recognizes EOF. for line in myfile: <do something with the line> Here we have to check for the end of file: line = myfile.readline() reads the next line and s = myfile.read(10) reads 10 characters The EOF character is the empty string.
Writing to a file First open the file for writing or appending: myfile = open( story.txt, w ) start the story myfile = open( story.txt, a ) continue the story Then write to the file: myfile.write( Once upon a time ) myfile.write( The end. ) Then close the file: myfile.close()
Reading from a CSV file Often we have comma-separated values data files: First Name, Last Name, Utorid Anna, Bretscher, bretsche Joe, Johnson, johnsonj Sally, Jordan, jordansa Why do we like CSV files? Spreadsheet applica@ons like excel understand it Many other programming languages also understand them Easy to generate ourselves
Reading from a CSV file How do we read comma-separated values data files: import io import csv csv_file = open( csv_filename.csv, r ) reader = csv.reader(csv_file) for line in reader: # read like an ordinary file # but line is a list
While Loops Sometimes we need to loop until a condition is met. For example: Ask user for a password twice If the passwords don t match, ask again until they match or the user quits
While Loop Example English example: Ask for password. Ask for password again. While the passwords don t match: Ask again.
While Loop Example Python example: password1 = input( Enter password: ) Ask for password again. While the passwords don t match: Ask again.
While Loop Example Python example: password1 = input( Enter password: ) password2 = input( Re-enter password: ) While the passwords don t match: Ask again.
While Loop Example Python example: password1 = input( Enter password: ) password2 = input( Re-enter password: ) while password1!= password2: Ask again.
While Loop Example Python example: password1 = input( Enter password: ) password2 = input( Re-enter password: ) while password1!= password2: password2 = input( Re-enter password: )