Skip to content

Commit e671a02

Browse files
committed
#12 - Python
1 parent 80bdf4e commit e671a02

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
#12 JSON Y XML
3+
"""
4+
5+
import json
6+
import os
7+
8+
# Datos a guardar
9+
datos = {
10+
"nombre": "cesar-ch",
11+
"edad": 3,
12+
"fechaNacimiento": "2021-02-03",
13+
"lenguajes": ["JavaScript", "Python", "Java"],
14+
}
15+
16+
# Guardar en archivo XML
17+
datosXML = f"""<?xml version="1.0" encoding="UTF-8"?>
18+
<datos>
19+
<nombre>{datos["nombre"]}</nombre>
20+
<edad>{datos["edad"]}</edad>
21+
<fechaNacimiento>{datos["fechaNacimiento"]}</fechaNacimiento>
22+
<lenguajes>{",".join(datos["lenguajes"])}</lenguajes>
23+
</datos>"""
24+
25+
with open("datos.xml", "w") as file:
26+
file.write(datosXML)
27+
print("Archivo XML creado")
28+
29+
# Guardar en archivo JSON
30+
with open("datos.json", "w") as file:
31+
json.dump(datos, file, indent=2)
32+
print("Archivo JSON creado")
33+
34+
"""
35+
DIFICULTAD EXTRA
36+
"""
37+
38+
# Leer archivo XML
39+
with open("datos.xml", "r") as file:
40+
data = file.read()
41+
print(data)
42+
43+
# Leer archivo JSON
44+
with open("datos.json", "r") as file:
45+
data = file.read()
46+
print(data)
47+
48+
# Borrar archivo XML
49+
os.remove("datos.xml")
50+
print("Archivo XML eliminado")
51+
52+
# Borrar archivo JSON
53+
os.remove("datos.json")
54+
print("Archivo JSON eliminado")

0 commit comments

Comments
 (0)