Reading the data

This section uses the same data file that we just saw. If you want to see all of the data click on the Show button below. Once it appears, you can hide it again by clicking on the Hide button.

If you clicked on the Show button you can tell that it is a lot of data. But we don’t actually know the number of lines of data in the file by looking at it. Since we don’t know the number of lines here, we won’t use a for loop to process it. We will use a while loop to keep reading the data until there isn’t any more (returns an empty string).

We can process it by opening the file containing the data, then reading each line in. As long as the line is not empty, we keep going doing a split and processing the data. (This data is separated by a : instead of a ,.)

In Python, we must open files before we can use them and close them when we are done with them. Opening a file returns a Python object that has predefined functions and procedures, just like the turtle objects we have seen before. Table 1 shows the functions and procedures that can be used to open and close files.

Method Name

Use

Explanation

open

open(filename,'r')

Open a file called filename and use it for reading. This will return a reference to a file object.

open

open(filename,'w')

Open a file called filename and use it for writing. This will also return a reference to a file object.

close

filename.close()

Closes the file after you are finished with it.

The code below will print out the pollution information for all the cities that start with "A". It will print the city, state on one line and the two pollution values for that city on another line. It stops looping when there aren’t any more lines to be read (when the returned line is an empty string).

There is actually a way of using a for loop to read in a file. We can read the whole thing into one giant list, and then use a for loop to process each line in the list.

Write code to that will read an input file uspoll.txt . It will print the city name and the pollution values for all cities that have a PM 10 pollution of 20 or more.

Show Comments

Note

Discuss topics in this section with classmates.

Show Comments
You have attempted of activities on this page