Skip to main content

Section 11.4 Iterating over lines in a file

Recall the contents of the ccdata.txt file.
Table 11.4.1.
Year Global Average Temp. (Celcius) Global Emmisions C02 (Giga-tons)
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.2.

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 of activities on this page.