Python Generator
Writing a generator to load data in chunks (2)
In the previous exercise, you processed a file line by line for a given number of lines. What if, however, you want to do this for the entire file?
In this case, it would be useful to use generators. Generators allow users to lazily evaluate data. This concept of lazy evaluation is useful when you have to deal with very large datasets because it lets you generate values in an efficient manner by yielding only chunks of data at a time instead of the whole thing at once.
In this exercise, you will define a generator function read_large_file()
that produces a generator object which yields a single line from a file each time next()
is called on it. The csv file 'world_dev_ind.csv'
is in your current directory for your use.
Note that when you open a connection to a file, the resulting file object is already a generator! So out in the wild, you won't have to explicitly create generator objects in cases such as this. However, for pedagogical reasons, we are having you practice how to do this here with the read_large_file()
function. Go for it!
- n the function
read_large_file()
, read a line fromfile_object
by using the methodreadline()
. Assign the result todata
. - In the function
read_large_file()
,yield
the line read from the filedata
. - In the context manager, create a generator object
gen_file
by calling your generator functionread_large_file()
and passingfile
to it. - Print the first three lines produced by the generator object
gen_file
usingnext()
.
Hint
- To apply a method
x()
to an objecty
, do:y.x()
. - Make sure you produce the line read from the file by using
yield data
inside thewhile
loop. - Pass
file
as an argument toread_large_file()
. - In the call to
next()
, pass the generator objectgen_file
. Pass this call to the threeprint()
calls to print one line after another.
Comments
Post a Comment