""" Ex 4.4 from "A primer on..." Read a sequence of temperatures in degrees Fahrenheit from a file, convert to Celsius and store them in two lists, then write both temperatures to a new file. """ #empty lists to be filled later: F = [] C = [] with open('fahrenheit2.txt') as infile: # skip first three lines: infile.readline() infile.readline() infile.readline() #use a for-loop to iterate through remaining lines: for line in infile: #split each line into words words = line.split() """ Variable names F_, C_ are used for individual temperatures, to avoid conflict with the names for the entire lists, defined above. """ F_ = float(words[-1]) C_ = (5 / 9) * (F_ - 32) F.append(F_) C.append(C_) """open a new file for writing, and use zip and a for-loop to traverse the two lists and write the contents to the file.""" with open('degrees.txt','w') as outfile: for F_, C_ in zip(F, C): outfile.write(f'{F_:5.1f} {C_:5.1f} \n') """ Terminal> python f2c_file_read_write.py Terminal> more degrees.txt 67.2 19.6 66.0 18.9 78.9 26.1 102.1 38.9 32.0 0.0 87.8 31.0 """