Power Point

advertisement
Last Week
• Looping though lists
• Looping using range
• While loops
This Week
• Special characters in strings
• Files – what are they?
• Opening and closing files
• Reading and writing to files
• Comma Separated Files
Invisible Characters
How do we indicate a tab, newline or ‘ or “
inside a string?
\t
\n
\’
tab
new line
single quote
\”
double 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 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
Opening Files
How do we open a file in Python?
import io
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 that 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?
This method 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 applications 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
Download