example.txt
¶%%file example.txt
This is an example file.
It has several lines of text.
Nothing terribly interesting.
open
¶%%file read_example.py
file = open('example.txt')
for line in file:
print(line)
file.close()
read_example.py
¶NOTES
open
open
is iterablefor
, that thing is iterable
NOTES
with
¶with open('example.txt') as file:
for line in file:
print(line)
NOTES
with open(...) as file
file
is the variable name - you can pick whatever you wantwith
closes the file at the end of the block (so you don't have to)with
when working with fileswith
isn't what you need, but they are raredef get_lines(filename):
with open(filename) as file:
lines = []
for line in file:
lines.append(line)
return lines
print(get_lines('example.txt'))
What is the \n
at the end of each line?
'\n'
¶We need a way to represent all characters in our code, including the ones you can't see (like space and newline).
In Python, this is done using the backslash character \
. We sometimes call this the escape character.
When you see a \
in a string, it means the character after it is special.
\n
does not mean "n", it means "newline".
print('I have a dog. Her name is Sally.')
print('I have a dog.\nHer name is Sally.')
Word to the wise
list(...)
¶def get_lines(filename):
with open(filename) as file:
lines = []
for line in file:
lines.append(line)
return lines
def get_lines(filename):
with open(filename) as file:
return list(file)
print(get_lines('example.txt'))
NOTES
for...in...
(i.e. an iterable) can be turned into a list using list(...)
get_lines
do you like more? Why?.split()
¶"this is a string containing several words\n".split()
NOTES
'1 2 3 4 5'.split()
How is
['1', '2', '3', '4', '5']
different from
[1, 2, 3, 4, 5]
NOTES
'1'
is different from 1
'1'
to 1
?int(...)
¶'7'
int('7')
int('12') * 2
float(...)
¶'7.0'
float('7.0')
float('7.0') * 2
int('7.0')
float('7')
int
or float
?¶Look at the file!
print_means.py
¶# Solution
def get_lines(filename):
"""Returns a list of the lines in the file."""
with open(filename) as file:
return list(file)
def get_numbers(line):
"""Parses out the integers in `line` into a list.
>>> get_numbers("1 2 3")
[1, 2, 3]
"""
tokens = line.split()
numbers = []
for token in tokens:
numbers.append(int(token))
return numbers
def average(numbers):
"""Computes the average of a list of numbers
>>> average([1, 2, 3])
2
"""
total = 0
for number in numbers:
total = total + number
return total / len(numbers)
def print_means(filename):
"""Prints the average value for each line in the file."""
for line in get_lines(filename):
numbers = get_numbers(line)
ave = average(numbers)
print(ave)
filename = 'data.txt'
print_means(filename)
NOTES
open
with ... as ...
\n
list(...)
.split()
int(...)
/ float(...)
.strip()
¶Sometimes you don't want newlines at the end of your strings.
def get_lines(filename):
with open(filename) as file:
lines = []
for line in file:
lines.append(line.strip())
return lines
get_lines('example.txt')