Download presentation
Presentation is loading. Please wait.
Published byGilbert Foster Modified over 9 years ago
1
Last Week Lists List methods Nested Lists Looping through lists using for loops
2
This Week and Maybe Next While loops Special characters in strings Files –What are they? –Opening and closing files –Reading and writing to files –Comma Separated Files
3
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
4
While Loop Example English example: Ask for password. Ask for password again. While the passwords don’t match: Ask again.
5
While Loop Example Python example: password1 = input(‘Enter password: ’) Ask for password again. While the passwords don’t match: Ask again.
6
While Loop Example Python example: password1 = input(‘Enter password: ’) password2 = input(‘Re-enter password: ’) While the passwords don’t match: Ask again.
7
While Loop Example Python example: password1 = input(‘Enter password: ’) password2 = input(‘Re-enter password: ’) while password1 != password2: Ask again.
8
While Loop Example Python example: password1 = input(‘Enter password: ’) password2 = input(‘Re-enter password: ’) while password1 != password2: password2 = input(‘Re-enter password:’)
9
Invisible Characters How do we indicate a tab, newline or ‘ or “ inside a string? \ttab\”double quote \nnew 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.
10
Files Files – what are they? –Can think of as a collection of stored information –Computer thinks as a sequence of bits possible 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
11
Opening Files How do we open a file in Python? import doctest 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)
12
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
13
Reading Files There are many ways to read from a file. 1.Using a for loop: myfile = open(‘filename’, ‘r’) for line in myfile: 2.Read the whole file at once into a list of strings: myfile = open(‘filename’, ‘r’) list_of_lines = myfile.readlines()
14
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
15
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
16
End Of File (EOF) How do we know when we reach the end of the file? This method automatically recognizes EOF. for line in myfile: 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 “”.
17
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()
18
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 applications like excel understand it –Many other programming languages also understand them –Easy to generate ourselves
19
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
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.