Advertisement
teslariu

arch

Oct 18th, 2021
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.53 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """
  5. open(), write(), read(), close(), writelines() readlines()
  6.  
  7. ========= ===============================================================
  8. Character Meaning
  9. --------- ---------------------------------------------------------------
  10.    'r'   open for reading (default)
  11.    'w'   open for writing, truncating the file first
  12.    'x'   create a new file and open it for writing
  13.    'a'   open for writing, appending to the end of the file if it exists
  14.    'b'   binary mode
  15.    't'   text mode (default)
  16.    '+'   open a disk file for updating (reading and writing)
  17.    'U'   universal newline mode (deprecated)
  18. ========= ===============================================================
  19.  
  20. """
  21. # creo un archivo vacío 'celsius.txt'
  22. f = open("celsius.txt","x")
  23.  
  24. # preparo los datos
  25. datos1 = ['10ºC\n', '22ºC\n', '-14.258ºC\n', '11ºC\n']
  26. datos2 = ['8ºC\n', '12.5ºC\n', '102ºC\n', '-0.2581ºC\n']
  27.  
  28.  
  29. # guardo los datos en el archivo
  30. # forma 1:
  31. f.writelines(datos1)
  32.  
  33. # forma 2:
  34. for dato in datos2:
  35.     f.write(dato)
  36.    
  37. # cierro el archivo
  38. f.close()
  39.  
  40. # abro un archivo para guardar los datos en ºF
  41. f = open("farenheit.txt", "x")
  42.  
  43. # abro el archivo "celsius.txt" para leer sus datos y pasarlos a ºF
  44. f2 = open("celsius.txt")
  45. contenido = f2.readlines()
  46.  
  47. # ya leí "celsius.txt", lo cierro
  48. f2.close()
  49.  
  50. # escribo el archivo "farenheit.txt"
  51. for fila in contenido:
  52.     temp, escala = fila.split("º")
  53.     temp = float(temp) * 1.8 + 32
  54.     f.write("{:.1f}ºF\n".format(temp))
  55. f.close()
  56.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement