|
| 1 | +import os |
| 2 | +import xml.etree.ElementTree as xml |
| 3 | +import json |
| 4 | + |
| 5 | +data = { |
| 6 | + "name": "Dkp Dev", |
| 7 | + "age": 29, |
| 8 | + "birth_date": "11-01-1995", |
| 9 | + "programming_languages": ["Python", "Kotlin", "Java"] |
| 10 | +} |
| 11 | + |
| 12 | +xml_file = "dkp-dev.xml" |
| 13 | +json_file = "dkp-dev.json" |
| 14 | + |
| 15 | +""" |
| 16 | +EJERCICIO: |
| 17 | + * Desarrolla un programa capaz de crear un archivo XML y JSON que guarde los |
| 18 | + * siguientes datos (haciendo uso de la sintaxis correcta en cada caso): |
| 19 | + * - Nombre |
| 20 | + * - Edad |
| 21 | + * - Fecha de nacimiento |
| 22 | + * - Listado de lenguajes de programación |
| 23 | + * Muestra el contenido de los archivos. |
| 24 | + * Borra los archivos. |
| 25 | +""" |
| 26 | + |
| 27 | +# XML |
| 28 | + |
| 29 | + |
| 30 | +def create_xml(): |
| 31 | + |
| 32 | + root = xml.Element("data") |
| 33 | + |
| 34 | + for key, value in data.items(): |
| 35 | + child = xml.SubElement(root, key) |
| 36 | + if isinstance(value, list): |
| 37 | + for item in value: |
| 38 | + xml.SubElement(child, "item").text = item |
| 39 | + else: |
| 40 | + child.text = str(value) |
| 41 | + |
| 42 | + tree = xml.ElementTree(root) |
| 43 | + tree.write(xml_file) |
| 44 | + |
| 45 | + |
| 46 | +create_xml() |
| 47 | + |
| 48 | +with open(xml_file, "r") as xml_data: |
| 49 | + print(xml_data.read()) |
| 50 | + |
| 51 | +os.remove(xml_file) |
| 52 | + |
| 53 | +# JSON |
| 54 | + |
| 55 | + |
| 56 | +def create_json(): |
| 57 | + with open(json_file, "w") as json_data: |
| 58 | + json.dump(data, json_data) |
| 59 | + |
| 60 | + |
| 61 | +create_json() |
| 62 | + |
| 63 | +with open(json_file, "r") as json_data: |
| 64 | + print(json_data.read()) |
| 65 | + |
| 66 | +os.remove(json_file) |
| 67 | + |
| 68 | +""" |
| 69 | + DIFICULTAD EXTRA (opcional): |
| 70 | + * Utilizando la lógica de creación de los archivos anteriores, crea un |
| 71 | + * programa capaz de leer y transformar en una misma clase custom de tu |
| 72 | + * lenguaje los datos almacenados en el XML y el JSON. |
| 73 | + * Borra los archivos. |
| 74 | +""" |
| 75 | + |
| 76 | +create_xml() |
| 77 | +create_json() |
| 78 | + |
| 79 | + |
| 80 | +class Data: |
| 81 | + |
| 82 | + def __init__(self, name, age, birth_date, programming_languages) -> None: |
| 83 | + self.name = name |
| 84 | + self.age = age |
| 85 | + self.birth_date = birth_date |
| 86 | + self.programming_languages = programming_languages |
| 87 | + |
| 88 | + |
| 89 | +with open(xml_file, "r") as xml_data: |
| 90 | + |
| 91 | + root = xml.fromstring(xml_data.read()) |
| 92 | + name = root.find("name").text |
| 93 | + age = root.find("age").text |
| 94 | + birth_date = root.find("birth_date").text |
| 95 | + programming_languages = [] |
| 96 | + for item in root.find("programming_languages"): |
| 97 | + programming_languages.append(item.text) |
| 98 | + |
| 99 | + xml_class = Data(name, age, birth_date, programming_languages) |
| 100 | + print(xml_class.__dict__) |
| 101 | + |
| 102 | + |
| 103 | +with open(json_file, "r") as json_data: |
| 104 | + json_dict = json.load(json_data) |
| 105 | + json_class = Data( |
| 106 | + json_dict["name"], |
| 107 | + json_dict["age"], |
| 108 | + json_dict["birth_date"], |
| 109 | + json_dict["programming_languages"] |
| 110 | + ) |
| 111 | + print(json_class.__dict__) |
| 112 | + |
| 113 | +os.remove(xml_file) |
| 114 | +os.remove(json_file) |
0 commit comments