Advertisement
teslariu

temp

Jul 24th, 2021
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.85 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. ========================================================================
  5.    Character Meaning
  6.    --------- ---------------------------------------------------------------
  7.    'r'       open for reading (default)
  8.    'w'       open for writing, truncating the file first
  9.    'x'       create a new file and open it for writing
  10.    'a'       open for writing, appending to the end of the file if it exists
  11.    'b'       binary mode
  12.    't'       text mode (default)
  13.    '+'       open a disk file for updating (reading and writing)
  14.    'U'       universal newline mode (deprecated)
  15. ========================================================================
  16.  
  17. Script que lee un archivo de texto con temp en ºC y crea un archivo con las mismas
  18. temperaturas expresadas en ºF
  19.  
  20. ºF = ºC * 1.8 + 32
  21.  
  22. """
  23. # creo un archivo vacío en modo escritura
  24. f = open("centigrados.txt", "x")
  25.  
  26. # creo una lista de datos para guardarlos en el archivo
  27. datos = ['10ºC\n', '12.5ºC\n', '14.2ºC\n', '-25ºC\n', '20.2ºC\n']
  28.  
  29. # escribo los datos en el archivo
  30. f.writelines(datos)
  31.                    
  32. # cierro el archivo
  33. f.close()
  34.  
  35. # supongo que me olvide un dato, lo agrego (en modo append):
  36. f = open("centigrados.txt", "a")
  37. f.write("11.5ºC\n")
  38.  
  39. # cierro f y lo abro como lectura
  40. f.close()
  41. f = open("centigrados.txt")
  42.  
  43. # actualizo la lista de datos
  44. datos = f.readlines()
  45.  
  46. # cierro el archivo
  47. f.close()
  48.  
  49.  
  50. # creo un archivo vacío en modo escritura
  51. f = open("farenheit.txt", "x")
  52.  
  53. # escribo los datos en el archivo
  54. for dato in datos:
  55.     temp, escala = dato.split("º")
  56.     temp = float(temp) * 1.8 + 32
  57.     f.write("{:.1f}ºF\n".format(temp))
  58. f.close()
  59.  
  60. # imprimo el archivo farenheit.txt:
  61. f = open("farenheit.txt")
  62. valores = f.readlines()
  63. f.close()
  64. for valor in valores:
  65.     print(valor,end="")
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement