informatica:programacion:python:ficheros
Diferencias
Muestra las diferencias entre dos versiones de la página.
| Próxima revisión | Revisión previa | ||
| informatica:programacion:python:ficheros [2021/11/05 13:17] – creado tempwin | informatica:programacion:python:ficheros [2021/11/05 13:32] (actual) – [Ficheros JSON] tempwin | ||
|---|---|---|---|
| Línea 2: | Línea 2: | ||
| ===== Leer ficheros ===== | ===== Leer ficheros ===== | ||
| + | |||
| + | ==== Texto normal ==== | ||
| Leerlo completamente, | Leerlo completamente, | ||
| Línea 23: | Línea 25: | ||
| print(contenido ) | print(contenido ) | ||
| + | </ | ||
| + | |||
| + | Para leer un fichero y almacenar sus líneas: | ||
| + | |||
| + | <code python> | ||
| + | fname = ' | ||
| + | with open(fname, ' | ||
| + | contenido = f.readlines() | ||
| + | |||
| + | print(contenido) | ||
| + | </ | ||
| + | |||
| + | El contenido se cargará como una lista de líneas. Lo que delimita una línea es el carácter fin de línea ('' | ||
| + | |||
| + | Para leer un fichero línea a línea (útil si es demasiado grande para guardar en memoria): | ||
| + | |||
| + | <code python> | ||
| + | fname = ' | ||
| + | with open(fname, ' | ||
| + | for linea in f: | ||
| + | print(linea) | ||
| + | </ | ||
| + | |||
| + | También podemos usar '' | ||
| + | |||
| + | <code python> | ||
| + | fname = ' | ||
| + | with open(fname, ' | ||
| + | for i, linea in enumerate(f): | ||
| + | print(" | ||
| + | </ | ||
| + | |||
| + | ==== Ficheros CSV ==== | ||
| + | |||
| + | Si el fichero contiene una línea con la cabecera (título de los campos): | ||
| + | |||
| + | <code python> | ||
| + | import csv | ||
| + | |||
| + | fname = ' | ||
| + | |||
| + | with open(fname, ' | ||
| + | data_reader = csv.reader(f, | ||
| + | headers = next(data_reader) | ||
| + | print(" | ||
| + | for line in data_reader: | ||
| + | print(line) | ||
| + | </ | ||
| + | |||
| + | Cada línea Python la representará como una lista. | ||
| + | |||
| + | Si el fichero no tiene cabecera: | ||
| + | |||
| + | <code python> | ||
| + | fname = ' | ||
| + | |||
| + | with open(fname, ' | ||
| + | data_reader = csv.reader(f, | ||
| + | for line in data_reader: | ||
| + | print(line) | ||
| + | </ | ||
| + | |||
| + | ==== Ficheros JSON ==== | ||
| + | |||
| + | JavaScript Object Notation. Formato muy utilizado en la serialización de los datos y la comunicación entre servicios. | ||
| + | |||
| + | Ejemplo de fichero JSON: | ||
| + | |||
| + | <code javascript> | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | "Brad Pitt", | ||
| + | " | ||
| + | " | ||
| + | ] | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Lectura de fichero JSON en Python: | ||
| + | |||
| + | <code python> | ||
| + | import json | ||
| + | |||
| + | fname = ' | ||
| + | with open(fname, ' | ||
| + | content = f.read() | ||
| + | movie = json.loads(content) | ||
| + | |||
| + | movie | ||
| + | |||
| + | # " | ||
| + | </ | ||
| + | |||
| + | Una alternativa más simple: | ||
| + | |||
| + | <code python> | ||
| + | import json | ||
| + | |||
| + | fname = ' | ||
| + | with open(fname, ' | ||
| + | movie = json.load(f) | ||
| + | |||
| + | movie | ||
| + | </ | ||
| + | |||
| + | Si queremos mostrar un elemento JSON de forma agradable por pantalla: | ||
| + | |||
| + | <code python> | ||
| + | print(json.dumps(movie, | ||
| </ | </ | ||
informatica/programacion/python/ficheros.1636114659.txt.gz · Última modificación: por tempwin
