Skip to main content

Section 10.4 Iterating over lines in a file

Recall the contents of the daily_weather.txt file (date, low temperature, and high temperature in ° Celsius):
Data: daily_weather.txt
2022-06-01,12,29
2022-06-02,13,25
2022-06-03,14,24
2022-06-04,15,24
2022-06-05,17,27
2022-06-06,14,25
2022-06-07,13,26
2022-06-08,14,26
2022-06-09,13,33
2022-06-10,17,36
2022-06-11,18,33
2022-06-12,15,27
2022-06-13,13,24
2022-06-14,14,30
2022-06-15,14,28
2022-06-16,12,23
2022-06-17,13,22
2022-06-18,11,23
2022-06-19,11,29
2022-06-20,13,31
2022-06-21,16,39
2022-06-22,21,36
2022-06-23,18,32
2022-06-24,17,31
2022-06-25,14,28
2022-06-26,15,27
2022-06-27,15,28
2022-06-28,14,28
2022-06-29,13,23
2022-06-30,14,23
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 find the month’s minimum and maximum temperatures. 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 the day’s weather. We can then take the values corresponding to the minimum and maximum temperature.

Note 10.4.1.

You can obtain a line from the keyboard with the input function, and you can process lines of a file. However the word “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 this 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. Also, we set min_temp and max_temp to None at first. When we get a new value for day_min from the file, line 9 says that if there is no minimum temperature yet (min_temp == None) or the new value is less than the current minimum (day_min < min_temp), the new value becomes the current minimum temperature in line 10. Lines 11 and 12 do the same logic for the maximum temperature.