|
| 1 | +""" |
| 2 | +Ejercicio |
| 3 | +""" |
| 4 | + |
| 5 | +import xml.etree.ElementTree as xml |
| 6 | +import xml.dom.minidom as minidom |
| 7 | +import os |
| 8 | + |
| 9 | +data = { |
| 10 | + "name" : "Luis", |
| 11 | + "age" : 25, |
| 12 | + "birthday" : "1995-09-19", |
| 13 | + "programming_languages" : ["Python", "HTML", "JavaScript"] |
| 14 | +} |
| 15 | + |
| 16 | +def create_xml(): |
| 17 | + root = xml.Element("data") |
| 18 | + |
| 19 | + for key, value in data.items(): |
| 20 | + |
| 21 | + child = xml.SubElement(root, key) |
| 22 | + if isinstance(value, list): |
| 23 | + for item in value: |
| 24 | + xml.SubElement(child, "Item").text = item |
| 25 | + else: |
| 26 | + child.text = str(value) |
| 27 | + |
| 28 | + tree = xml.ElementTree(root) |
| 29 | + tree.write("programador.xml") |
| 30 | + |
| 31 | + #Convert the tree to a string |
| 32 | + xml_str = xml.tostring(root, encoding='unicode') |
| 33 | + |
| 34 | + #Parse the string using a minidom for pretty printing |
| 35 | + dom = minidom.parseString(xml_str) |
| 36 | + pretty_xml_as_string = dom.toprettyxml() |
| 37 | + |
| 38 | + with open("programador.xml", "w") as f: |
| 39 | + f.write(pretty_xml_as_string) |
| 40 | + |
| 41 | +create_xml() |
| 42 | + |
| 43 | +with open("programador.xml", 'r') as xml_data: |
| 44 | + print(xml_data.read()) |
| 45 | + |
| 46 | +#os.remove("programador.xml") |
| 47 | + |
| 48 | + |
| 49 | +""" |
| 50 | +Json |
| 51 | +""" |
| 52 | + |
| 53 | +import json |
| 54 | + |
| 55 | +json_file = "programador1.json" |
| 56 | + |
| 57 | +def create_json(): |
| 58 | + with open(json_file, "w") as f: |
| 59 | + json.dump(data, f, indent=4) |
| 60 | + |
| 61 | +create_json() |
| 62 | + |
| 63 | +with open(json_file, "r") as f: |
| 64 | + print(f.read()) |
| 65 | + |
| 66 | +#os.remove(json_file) |
| 67 | + |
| 68 | +""" |
| 69 | +Extra |
| 70 | +""" |
| 71 | + |
| 72 | +create_xml() |
| 73 | +create_json() |
| 74 | + |
| 75 | +class Data: |
| 76 | + |
| 77 | + def __init__(self, name, age, birthday, programming_languages) -> None: |
| 78 | + self.name = name |
| 79 | + self.age = age |
| 80 | + self.birthday = birthday |
| 81 | + self.programming_languages = programming_languages |
| 82 | + |
| 83 | + |
| 84 | +with open("programador.xml", "r") as f: |
| 85 | + root = xml.fromstring(f.read()) |
| 86 | + name = root.find("name").text |
| 87 | + age = root.find("age").text |
| 88 | + birthday = root.find("birthday").text |
| 89 | + programming_languages = [] |
| 90 | + for item in root.find("programming_languages"): |
| 91 | + programming_languages.append(item.text) |
| 92 | + |
| 93 | + data_class = Data(name, age, birthday, programming_languages) |
| 94 | + print(data_class.__dict__) |
| 95 | + |
| 96 | + |
| 97 | +#json |
| 98 | + |
| 99 | +with open(json_file, "r") as f: |
| 100 | + json_dict = json.load(f) |
| 101 | + json_class = Data( |
| 102 | + json_dict["name"], |
| 103 | + json_dict["age"], |
| 104 | + json_dict["birthday"], |
| 105 | + json_dict["programming_languages"] |
| 106 | + ) |
| 107 | + print(json_class.__dict__) |
| 108 | + |
| 109 | + |
| 110 | +os.remove("programador.xml") |
| 111 | +os.remove(json_file) |
0 commit comments