|
| 1 | +"""EJERCICIO: |
| 2 | +Desarrolla un programa capaz de crear un archivo XML y JSON que guarde los |
| 3 | +siguientes datos (haciendo uso de la sintaxis correcta en cada caso): |
| 4 | +- Nombre |
| 5 | +- Edad |
| 6 | +- Fecha de nacimiento |
| 7 | +- Listado de lenguajes de programación |
| 8 | +Muestra el contenido de los archivos. |
| 9 | +Borra los archivos.""" |
| 10 | + |
| 11 | +import json |
| 12 | +import os |
| 13 | +import xml.etree.ElementTree as ET |
| 14 | + |
| 15 | + |
| 16 | +class JsonFile: |
| 17 | + def __init__(self, path): |
| 18 | + self.file_path = f'{path}.json' |
| 19 | + |
| 20 | + def create_json(self): |
| 21 | + data = { |
| 22 | + 'name': "Brandon", |
| 23 | + 'age': 20, |
| 24 | + 'dob': "05/20/1994", |
| 25 | + 'language': ['Python', 'Matlab', 'C#'] |
| 26 | + } |
| 27 | + with open(self.file_path, "w") as json_file: |
| 28 | + json.dump(data, json_file, indent=4) |
| 29 | + |
| 30 | + def show_json(self): |
| 31 | + with open(self.file_path, "r") as json_file: |
| 32 | + data = json.load(json_file) |
| 33 | + print(json.dumps(data, indent=4)) |
| 34 | + |
| 35 | + def remove_json(self): |
| 36 | + if os.path.exists(self.file_path): |
| 37 | + os.remove(self.file_path) |
| 38 | + print(f"The file '{self.file_path}' has been removed.") |
| 39 | + else: |
| 40 | + print(f"The file '{self.file_path}' does not exist.") |
| 41 | + |
| 42 | + |
| 43 | +class XmlFile: |
| 44 | + def __init__(self, path): |
| 45 | + self.file_path = f'{path}.xml' |
| 46 | + |
| 47 | + def create_xml(self): |
| 48 | + root = ET.Element('data') |
| 49 | + |
| 50 | + name = ET.SubElement(root, 'name') |
| 51 | + name.text = 'Brandon' |
| 52 | + age = ET.SubElement(root,'age') |
| 53 | + age.text = '29' |
| 54 | + dob = ET.SubElement(root, 'dob') |
| 55 | + dob.text = '05/20/1994' |
| 56 | + language = ET.SubElement(root, 'language') |
| 57 | + language.text = str(['Python', 'Matlab', 'C#']) |
| 58 | + |
| 59 | + tree = ET.ElementTree(root) |
| 60 | + |
| 61 | + tree.write(self.file_path, encoding="utf-8", xml_declaration=True) |
| 62 | + |
| 63 | + def show_xml(self): |
| 64 | + tree = ET.parse(self.file_path) |
| 65 | + |
| 66 | + root = tree.getroot() |
| 67 | + |
| 68 | + for child in root: |
| 69 | + print(f'{child.tag}: {child.text}') |
| 70 | + |
| 71 | + def remove_xml(self): |
| 72 | + if os.path.exists(self.file_path): |
| 73 | + os.remove(self.file_path) |
| 74 | + print(f"The file '{self.file_path}' has been removed.") |
| 75 | + else: |
| 76 | + print(f"The file '{self.file_path}' does not exist.") |
| 77 | + |
| 78 | +"""DIFICULTAD EXTRA (opcional): |
| 79 | +Utilizando la lógica de creación de los archivos anteriores, crea un |
| 80 | +programa capaz de leer y transformar en una misma clase custom de tu |
| 81 | +lenguaje los datos almacenados en el XML y el JSON. |
| 82 | +Borra los archivos.""" |
| 83 | + |
| 84 | + |
| 85 | +def print_transformed_information(name, age, dob, language): |
| 86 | + print(f'Name: {name}\n' |
| 87 | + f'Age: {age}\n' |
| 88 | + f'Date of Birth: {dob}\n' |
| 89 | + f'Language: {language}') |
| 90 | + |
| 91 | + |
| 92 | +def read_transform_json(path): |
| 93 | + file_path = f'{path}.json' |
| 94 | + |
| 95 | + with open(file_path, 'r') as json_file: |
| 96 | + data = json.load(json_file) |
| 97 | + |
| 98 | + name = data['name'] |
| 99 | + age = data['age'] |
| 100 | + dob = data['dob'] |
| 101 | + languages = data['language'] |
| 102 | + print_transformed_information(name, age, dob, languages) |
| 103 | + |
| 104 | + |
| 105 | +def read_transform_xml(path): |
| 106 | + file_path = f'{path}.xml' |
| 107 | + tree = ET.parse(file_path) |
| 108 | + root = tree.getroot() |
| 109 | + |
| 110 | + name = root.find('name').text |
| 111 | + age = root.find('age').text |
| 112 | + dob = root.find('dob').text |
| 113 | + language = root.find('language').text |
| 114 | + print_transformed_information(name, age, dob, language) |
| 115 | + |
| 116 | + |
| 117 | +json_path = 'myjson' |
| 118 | +xml_path = 'myxml' |
| 119 | + |
| 120 | +print('******** JSON ********') |
| 121 | + |
| 122 | +json_file = JsonFile(json_path) |
| 123 | +json_file.create_json() |
| 124 | +print('\n**** Transformed values from the JSON file ****') |
| 125 | +read_transform_json(json_path) |
| 126 | +print('\n**** JSON File ****') |
| 127 | +json_file.show_json() |
| 128 | +json_file.remove_json() |
| 129 | + |
| 130 | +print('\n******** XML ********') |
| 131 | + |
| 132 | +xml_file = XmlFile(xml_path) |
| 133 | +xml_file.create_xml() |
| 134 | +print('\n**** Transformed values from the XML file ****') |
| 135 | +read_transform_xml(xml_path) |
| 136 | +print('\n**** XML File ****') |
| 137 | +xml_file.show_xml() |
| 138 | +xml_file.remove_xml() |
0 commit comments