|
| 1 | +#JSON AND XML |
| 2 | + |
| 3 | +#Creacion de fichero XML |
| 4 | + |
| 5 | +datos = {"nombre": "jptxaya", |
| 6 | + "edad": "45", |
| 7 | + "fecha_nacimiento": "19/03/1979", |
| 8 | + "lenguajes": ["python","java","react"]} |
| 9 | + |
| 10 | +#XML |
| 11 | + |
| 12 | +#Creacion de fichero XML |
| 13 | +import xml.etree.ElementTree as gfg |
| 14 | +import os |
| 15 | +import json |
| 16 | + |
| 17 | + |
| 18 | +try: |
| 19 | + root = gfg.Element("Datos") |
| 20 | + for key,value in datos.items(): |
| 21 | + elem = gfg.SubElement(root,key) |
| 22 | + if isinstance(value,list): |
| 23 | + i = 0 |
| 24 | + for value_list in value: |
| 25 | + lang = gfg.SubElement(elem,"lenguaje_" + str(i)) |
| 26 | + lang.text = value_list |
| 27 | + i += 1 |
| 28 | + else: |
| 29 | + elem.text = value |
| 30 | + tree = gfg.ElementTree(root) |
| 31 | + tree.write("newxml.xml", encoding="UTF-8", xml_declaration=True) |
| 32 | + |
| 33 | + |
| 34 | +# Read from XML file |
| 35 | + with open("newxml.xml", "r") as xml_read: |
| 36 | + root = gfg.fromstring(xml_read.read()) |
| 37 | + nombre = root.find("nombre").text |
| 38 | + edad = root.find("edad").text |
| 39 | + birth = root.find("fecha_nacimiento").text |
| 40 | + lenguajes = [] |
| 41 | + for language in root.find("lenguajes"): |
| 42 | + lenguajes.append(language.text) |
| 43 | + |
| 44 | + print(f"Nombre: {nombre} Edad:{edad} Fecha Nac:{birth}") |
| 45 | + print("Lenguajes:") |
| 46 | + for leng in lenguajes: |
| 47 | + print(leng) |
| 48 | + |
| 49 | +#Borrado fichero xml |
| 50 | +# os.remove("newxml.xml") |
| 51 | + |
| 52 | +except Exception as exc: |
| 53 | + print(f"Exception {exc}") |
| 54 | + |
| 55 | +#JSON |
| 56 | + |
| 57 | +try: |
| 58 | +#Creacion de Fichero json |
| 59 | + json_object = json.dumps(datos,indent=4) |
| 60 | + with open("newjson.json","w") as new_json: |
| 61 | + new_json.write(json_object) |
| 62 | + |
| 63 | +#Read File json |
| 64 | + with open("newjson.json","r") as new_json: |
| 65 | + json_string = new_json.read() |
| 66 | + json_object = json.loads(json_string) |
| 67 | + |
| 68 | + |
| 69 | + nombre = json_object["nombre"] |
| 70 | + edad = json_object["edad"] |
| 71 | + birth = json_object["fecha_nacimiento"] |
| 72 | + lenguajes = [] |
| 73 | + for language in json_object["lenguajes"]: |
| 74 | + lenguajes.append(language) |
| 75 | + |
| 76 | + print(f"Nombre: {nombre} Edad:{edad} Fecha Nac:{birth}") |
| 77 | + print("Lenguajes:") |
| 78 | + for leng in lenguajes: |
| 79 | + print(leng) |
| 80 | + |
| 81 | +#Borrado fichero json |
| 82 | +# os.remove("newjson.json") |
| 83 | + |
| 84 | +except Exception as exc: |
| 85 | + print(f"Exception {exc}") |
| 86 | + |
| 87 | + |
| 88 | +#Dificultad Extra |
| 89 | + |
| 90 | +print ("#######Dificultad extra") |
| 91 | + |
| 92 | +class Persona: |
| 93 | + |
| 94 | + def __init__(self,file) -> None: |
| 95 | + datos = self.read_file(file) |
| 96 | + self.name = datos["name"] |
| 97 | + self.age = datos["age"] |
| 98 | + self.birth = datos["birth"] |
| 99 | + self.languages = datos["languages"] |
| 100 | + |
| 101 | + |
| 102 | + def read_file(self, file): |
| 103 | + datos = {} |
| 104 | + if file.endswith(".xml"): |
| 105 | + with open(file, "r") as xml_read: |
| 106 | + root = gfg.fromstring(xml_read.read()) |
| 107 | + nombre = root.find("nombre").text |
| 108 | + edad = root.find("edad").text |
| 109 | + birth = root.find("fecha_nacimiento").text |
| 110 | + lenguajes = [] |
| 111 | + for language in root.find("lenguajes"): |
| 112 | + lenguajes.append(language.text) |
| 113 | + datos = {"name": nombre, "age": edad, "birth": birth, "languages": lenguajes} |
| 114 | + |
| 115 | + else: |
| 116 | + with open(file,"r") as new_json: |
| 117 | + json_string = new_json.read() |
| 118 | + json_object = json.loads(json_string) |
| 119 | + datos = {"name": json_object["nombre"], "age": json_object["edad"], "birth": json_object["fecha_nacimiento"], "languages": json_object["lenguajes"]} |
| 120 | + return datos |
| 121 | + |
| 122 | + def __str__(self) -> str: |
| 123 | + part1 = "Nombre:" + self.name + " Edad:" + self.age + " Fecha Nacimiento:" + self.birth + "\n" + "Lenguages" |
| 124 | + part2 = "" |
| 125 | + for leng in self.languages: |
| 126 | + part2 = part2 + "," + leng |
| 127 | + return part1 + part2 |
| 128 | + |
| 129 | +try: |
| 130 | + print("File xml") |
| 131 | + person1 = Persona("newxml.xml") |
| 132 | + print(str(person1)) |
| 133 | + print("File json") |
| 134 | + person2 = Persona("newjson.json") |
| 135 | + print(str(person2)) |
| 136 | +except Exception as ex: |
| 137 | + print("File not support or format not correct") |
| 138 | + |
| 139 | +os.remove("newxml.xml") |
| 140 | +os.remove("newjson.json") |
| 141 | + |
| 142 | + |
0 commit comments