Skip to main content

Section 11.4 Iterating over lines in a file

As an example, suppose we have a text file called ccdata.txt that contains the following data representing statistics about climate change. Although it would be possible to consider entering this data by hand each time it is used, you can imagine that it would be time-consuming and error-prone to do this. In addition, it is likely that there could be data from more sources and other years. The format of the data file is as follows:
Year, Global Average Temperature, Global Emmision of CO2
To open this file, we would call the open function. The variable, fileref, now holds a reference to the file object returned by open. Again, after the file is closed any further attempts to use fileref will result in an error.
with open("ccdata.txt", "r") as fileref:
  # do things
We will now use this file as input in a program that will do some data processing. In the program, we will read each line of the file and print it with some additional text. Because text files are sequences of lines of text, we can use the for loop to iterate through each line of the file.
A line of a file is defined to be a sequence of characters up to and including a special character called the newline character. If you evaluate a string that contains a newline character you will see the character represented as \n. If you print a string that contains a newline you will not see the \n, you will just see its effects. When you are typing a Python program and you press the enter or return key on your keyboard, the editor inserts a newline character into your text at that point.
As the for loop iterates through each line of the file the loop variable will contain the current line of the file as a string of characters. The general pattern for processing each line of a text file is as follows:
for line in myFile:
    statement1
    statement2
    ...
To process all of our climate change data, we will use a for loop to iterate over the lines of the file. Using the split method, we can break each line into a list containing all the fields of interest about climate change. We can then take the values corresponding to year, global average temperature, and global emmisions to construct a simple sentence.

Note 11.4.1.

You can obtain a line from the keyboard with the input function, and you can process lines of a file. However “line” is used differently: With input Python reads through the newline you enter from the keyboard, but the newline ('\n') is not included in the line returned by input. It is dropped. When a line is taken from a file, the terminating newline is included as the last character (unless you are reading the final line of a file that happens to not have a newline at the end).
In the climate change example it is irrelevant whether the final line has a newline character at the end or not, since it would be stripped off by the split method call.
You have attempted 1 of 2 activities on this page.