Skip to content

Commit 0eef663

Browse files
#12 - Python
1 parent a47b826 commit 0eef663

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import json
2+
import os
3+
import xml.etree.ElementTree as ET
4+
5+
# json extension
6+
7+
dct = {
8+
"firstname": "Duban",
9+
"lastname": "Quiroga",
10+
"country": "Colombia",
11+
"skills": [
12+
"Python", "Java"
13+
]
14+
}
15+
16+
def file_creates_and_read(path:str, dct:dict) -> str:
17+
18+
with open("./jsonfile.json", "w") as file:
19+
json.dump(dct, file, indent=4)
20+
21+
22+
#with open("./jsonfile.json") as file:
23+
# content = json.load(file)
24+
#os.remove(path)
25+
#return json.dumps(content, indent=4)
26+
27+
28+
print(file_creates_and_read("jsonfile.json", dct))
29+
30+
# XML extension
31+
32+
def create_file():
33+
# Crear el elemento raiz
34+
root = ET.Element("person")
35+
36+
# Añadir subelementos
37+
firstname = ET.SubElement(root, "firstname")
38+
firstname.text = "Duban"
39+
40+
lastname = ET.SubElement(root, "lastname")
41+
lastname.text = "Quiroga"
42+
43+
country = ET.SubElement(root, "country")
44+
country.text = "Colombia"
45+
46+
# Se crea un subelemento para los skills con una lista de lenguajes
47+
skills = ET.SubElement(root, "skills")
48+
skill1 = ET.SubElement(skills, "skill")
49+
skill1.text = "Python"
50+
skill2 = ET.SubElement(skills, "skill")
51+
skill2.text = "Java"
52+
53+
# Se guarda el archivo
54+
tree = ET.ElementTree(root)
55+
with open("person.xml", "wb") as f:
56+
tree.write(f)
57+
58+
def show_content():
59+
#Mostrar el contenido del archivo
60+
print("-" * 5, "Contenido del archivo XML", "-" * 5)
61+
tree = ET.parse("person.xml")
62+
root = tree.getroot()
63+
for element in root:
64+
if element.tag == "skills":
65+
print("Skills: ", [skill.text for skill in element])
66+
else:
67+
print(f"{element.tag.capitalize()}: {element.text}")
68+
69+
def remove_file():
70+
# Eliminar archivo
71+
if os.path.exists("person.xml"):
72+
os.remove("person.xml")
73+
else:
74+
print("El archivo no existe")
75+
76+
77+
""" Extra """
78+
79+
create_file()
80+
81+
82+
class Custom:
83+
84+
def __init__(self, fname, lname, country, skills):
85+
self.fname = fname
86+
self.lname = lname
87+
self.country = country
88+
self.skills = skills
89+
90+
file_name = "jsonfile.json"
91+
xml_file = "person.xml"
92+
93+
with open(file_name) as file:
94+
content = file.read()
95+
data_json = json.loads(content)
96+
person_json = Custom(data_json["firstname"], data_json["lastname"], data_json["country"], data_json["skills"])
97+
98+
99+
print(person_json.__dict__)
100+
101+
with open(xml_file, "r") as file:
102+
data = file.read()
103+
root = ET.fromstring(data)
104+
fname = root.find("firstname").text
105+
lname = root.find("lastname").text
106+
country = root.find("country").text
107+
skills = []
108+
for skill in root.find("skills"):
109+
skills.append(skill.text)
110+
111+
person_xml = Custom(fname, lname, country, skills)
112+
print(person_xml.__dict__) # Datos en formato diccionario
113+
114+
os.remove(file_name)
115+
os.remove(xml_file)

0 commit comments

Comments
 (0)