Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- """
- ========================================================================
- Character Meaning
- --------- ---------------------------------------------------------------
- 'r' open for reading (default)
- 'w' open for writing, truncating the file first
- 'x' create a new file and open it for writing
- 'a' open for writing, appending to the end of the file if it exists
- 'b' binary mode
- 't' text mode (default)
- '+' open a disk file for updating (reading and writing)
- 'U' universal newline mode (deprecated)
- ========================================================================
- Script que lee un archivo de texto con temp en ºC y crea un archivo con las mismas
- temperaturas expresadas en ºF
- ºF = ºC * 1.8 + 32
- """
- # creo un archivo vacío en modo escritura
- f = open("centigrados.txt", "x")
- # creo una lista de datos para guardarlos en el archivo
- datos = ['10ºC\n', '12.5ºC\n', '14.2ºC\n', '-25ºC\n', '20.2ºC\n']
- # escribo los datos en el archivo
- f.writelines(datos)
- # cierro el archivo
- f.close()
- # supongo que me olvide un dato, lo agrego (en modo append):
- f = open("centigrados.txt", "a")
- f.write("11.5ºC\n")
- # cierro f y lo abro como lectura
- f.close()
- f = open("centigrados.txt")
- # actualizo la lista de datos
- datos = f.readlines()
- # cierro el archivo
- f.close()
- # creo un archivo vacío en modo escritura
- f = open("farenheit.txt", "x")
- # escribo los datos en el archivo
- for dato in datos:
- temp, escala = dato.split("º")
- temp = float(temp) * 1.8 + 32
- f.write("{:.1f}ºF\n".format(temp))
- f.close()
- # imprimo el archivo farenheit.txt:
- f = open("farenheit.txt")
- valores = f.readlines()
- f.close()
- for valor in valores:
- print(valor,end="")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement