After reading lesson 23, you’ll be able to
- Write a function to read a file line by line
- Save numbers and strings from the file in variables
- Write a function to analyze the stored information
The only two ways you’ve seen so far to input data are to (1) to predefine variables in your program or (2) to ask the user to input data one-by-one. But when users have a lot of information to input into your program, you can’t expect them to enter it in real time. It’s often useful to have them give you the information in a file.
Computers are great at doing many computations quickly. A natural use for computers is to write programs that can read in large amounts of data from files and to perform simple analyses on that data. For example, you can export your own data from Microsoft Excel spreadsheets as files, or you can download data (such as weather or election data). After you’re given a file structured in a certain way, you can use knowledge of the structure to write a program to sequentially read and store the information from the file. With the data stored in your program, you can analyze it (for example, to find averages, maximums/minimums, and duplicates).
In addition to reviewing the concepts in this unit, this lesson will show you how to read data from a file.