Section 10.6 Writing Text Files
One of the most commonly performed data processing tasks is to read data from a file, manipulate it in some way, and then write the resulting data out to a new data file to be used for other purposes later. To accomplish this, the
open
function discussed above can also be used to create a new file prepared for writing. Note in
Section 10.1 that the only difference between opening a file for writing and opening a file for reading is the use of the
'w'
flag instead of the
'r'
flag as the second argument. When we open a file for writing, a new, empty file with that name is created and made ready to accept our data. As before, the function returns a reference to the new file object.
Section 10.5 shows one additional file method that we have not used thus far. The
write
method allows us to add data to a text file. Recall that text files contain sequences of characters. We usually think of these character sequences as being the lines of the file where each line ends with the newline
\n
character. Be very careful to notice that the
write
method takes one parameter, a string. When invoked, the characters of the string will be added to the end of the file. This means that it is the programmer’s job to include the newline characters as part of the string if desired.
As an example, consider the daily_weather.txt
file once again. Assume that we have been asked to provide a file consisting of the same data, but with the temperatures in Fahrenheit instead of Celsius. We also want the items on each line to be separated by a space instead of a comma.
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
To construct the new file, we will approach the problem using a similar algorithm as before. After opening the file, we will iterate through the lines, break each line into its parts, convert the parts that we need, and then output them to a file.
The program below solves part of the problem. We will print the output to the screen, since activecode doesn’t play nicely with writing files.
When we run this program, we see the lines of output on the screen. Once we are satisfied that it is creating the appropriate output, the next step is to add the necessary pieces to produce an output file and write the data lines to it. To start, we need to open a new output file by adding another call to the open
function, outfile = open("daily_fahrenheit.txt",'w')
, using the 'w'
flag. We can choose any file name we like. If the file does not exist, it will be created. However, if the file does exist, it will be reinitialized as empty and you will lose any previous contents.
Once the file has been created, we just need to call the write
method passing the string that we wish to add to the file. In this case, the string is already being printed so we will just change the print
into a call to the write
method. However, there is one additional part of the data line that we need to include. The newline character needs to be concatenated to the end of the line because, unlike print
, the write
method does not automatically add a newline to its output. We also need to close the file when we are done.
The complete program is shown below. Copy and paste it into your development environment and give it a try. You can copy and paste the temperature data from this page and put it into a file named daily_weather.txt
.
in_file = open("daily_weather.txt", "r")
out_file = open("daily_fahrenheit.txt", "w")
for line in in_file:
items = line.split(',')
min_temp_f = round(int(items[1]) * 9 / 5 + 32)
max_temp_f = round(int(items[2]) * 9 / 5 + 32)
out_file.write(f"{items[0]} {min_temp_f} {max_temp_f}\n")
in_file.close()
out_file.close()