Advertisement
teslariu

archivos

Apr 5th, 2022
1,240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.47 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.  
  22. # creo un archivo vacío para agregarle datos
  23. f = open("centigrados.txt","w")
  24. datos = ["25ºC\n", "-15.258ºC\n", "25.1ºC\n", "0ºC\n"]
  25.  
  26. # escribo cada elemento de dato como una línea
  27. f.writelines(datos)
  28. f.close()
  29.  
  30. # abro el archivo en modo lectura para mostrar su contenido
  31. f = open("centigrados.txt")
  32. lineas = f.readlines()
  33. for linea in lineas:
  34.     print(linea.strip())
  35.  
  36. # creo el otro
  37. f = open("farenheit.txt","w")
  38. for dato in datos:
  39.     temp, escala = dato.split("º")
  40.     temp = float(temp) * 1.8 + 32
  41.     f.write(f"{temp:.1f}ºF\n")
  42.  
  43. # cierro todo
  44. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement