Advertisement
teslariu

sentencia with

Apr 7th, 2022
1,204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.67 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Script que crea un archivo con temperaturas expresadas en ºC, luego genera otro archivo
  5. con sus temperaturas equivalentes en ºF
  6. ========= ===============================================================
  7.    Character Meaning
  8.    --------- ---------------------------------------------------------------
  9.    'r'       open for reading (default)
  10.    'w'       open for writing, truncating the file first
  11.    'x'       create a new file and open it for writing
  12.    'a'       open for writing, appending to the end of the file if it exists
  13.    'b'       binary mode
  14.    't'       text mode (default)
  15.    '+'       open a disk file for updating (reading and writing)
  16.    'U'       universal newline mode (deprecated)
  17.    ========= ===============================================================
  18.  
  19.    The default mode is 'rt' (open for reading text).
  20. """
  21. datos = ["25ºC\n", "-15.258ºC\n", "25.1ºC\n", "0ºC\n"]
  22.  
  23. # creo un archivo vacío para agregarle datos
  24. f = open("centigrados.txt","w")
  25. # escribo cada elemento de dato como una línea
  26. f.writelines(datos)
  27. f.close()
  28.  
  29.  
  30. # forma alternativa para abrir archivos (recomendada) en lugar de las lineas 24 a 27
  31. # ESTA FORMA NO NECESITA f.close()
  32. # with open("centigrados.txt","w"") as f:
  33. #   f.writelines(datos)
  34.  
  35. # abro el archivo en modo lectura para mostrar su contenido
  36. f = open("centigrados.txt")
  37. lineas = f.readlines()
  38. for linea in lineas:
  39.     print(linea.strip())
  40.  
  41. # creo el otro
  42. f = open("farenheit.txt","w")
  43. for dato in datos:
  44.     temp, escala = dato.split("º")
  45.     temp = float(temp) * 1.8 + 32
  46.     f.write(f"{temp:.1f}ºF\n")
  47.  
  48. # cierro todo
  49. f.close()
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement