1
+ #/*
2
+ # * IMPORTANTE: Sólo debes subir el fichero de código como parte del ejercicio.
3
+ # *
4
+ # * EJERCICIO:
5
+ # * Desarrolla un programa capaz de crear un archivo XML y JSON que guarde los
6
+ # * siguientes datos (haciendo uso de la sintaxis correcta en cada caso):
7
+ # * - Nombre
8
+ # * - Edad
9
+ # * - Fecha de nacimiento
10
+ # * - Listado de lenguajes de programación
11
+ # * Muestra el contenido de los archivos.
12
+ # * Borra los archivos.
13
+
14
+ import json
15
+ import os
16
+ import xml .etree .ElementTree as ET
17
+
18
+ def crear_archivo (extension : str ):
19
+ if extension == "json" :
20
+ with open (f"archivo.json" , "w" ) as arc :
21
+ contenido = {
22
+ "Nombre" : "Samy" ,
23
+ "Edad" : "19" ,
24
+ "FechaNacimiento" : "10/04/2004" ,
25
+ "Lenguajes" : ["Python" , "C#" , "Javascript" , "Java" , "PHP" ]
26
+ }
27
+ json .dump (contenido , arc )
28
+ elif extension == "xml" :
29
+ contenido = ET .Element ("contenido" )
30
+ ET .SubElement (contenido , "Nombre" ).text = "Samy"
31
+ ET .SubElement (contenido , "Edad" ).text = "19"
32
+ ET .SubElement (contenido , "FechaNacimiento" ).text = "10/04/2004"
33
+ ET .SubElement (contenido , "Lenguajes" ).text = "Python, C#, Javascript, Java, PHP"
34
+
35
+ xml = ET .ElementTree (contenido )
36
+ xml .write ("archivo.xml" , encoding = "UTF-8" , xml_declaration = True )
37
+ else :
38
+ print ("Extención no válida" )
39
+
40
+ crear_archivo ("json" )
41
+ crear_archivo ("xml" )
42
+ crear_archivo ("samy" )
43
+
44
+ with open ("archivo.json" , "r" ) as arc :
45
+ print (arc .read ())
46
+ with open ("archivo.xml" , "r" ) as arc :
47
+ print (arc .read ())
48
+
49
+ os .remove ("archivo.json" )
50
+ os .remove ("archivo.xml" )
51
+
52
+ # *
53
+ # * DIFICULTAD EXTRA (opcional):
54
+ # * Utilizando la lógica de creación de los archivos anteriores, crea un
55
+ # * programa capaz de leer y transformar en una misma clase custom de tu
56
+ # * lenguaje los datos almacenados en el XML y el JSON.
57
+ # * Borra los archivos.
58
+ # */
59
+
60
+ class fabrica_de_archivos :
61
+ def __init__ (self , nombre_archivo : str , extension : str , datos : dict ):
62
+ self .nombre_archivo = nombre_archivo
63
+ self .extension = extension
64
+ self .datos = datos
65
+
66
+ def crear_json (self ):
67
+ with open (f"{ self .nombre_archivo } .json" , "w" ) as arc :
68
+ json .dump (self .datos , arc )
69
+
70
+ def crear_xml (self ):
71
+ xml = ET .Element ("datos" )
72
+ for llave , valor in self .datos .items ():
73
+ ET .SubElement (xml , llave ).text = valor
74
+ arc = ET .ElementTree (xml )
75
+ arc .write (f"{ self .nombre_archivo } .xml" , encoding = "UTF-8" , xml_declaration = True )
76
+
77
+ def crear_archivo (self ):
78
+ match self .extension :
79
+ case "json" :
80
+ self .crear_json ()
81
+ case "xml" :
82
+ self .crear_xml ()
83
+ case _:
84
+ print ("Extención na válida" )
85
+
86
+ def leer_archivo (self ):
87
+ with open (f"{ self .nombre_archivo } .{ self .extension } " , "r" ) as arc :
88
+ print (arc .read ())
89
+
90
+ def borrar_archivo (self ):
91
+ os .remove (f"{ self .nombre_archivo } .{ self .extension } " )
92
+
93
+ def programa ():
94
+ diccionario = {
95
+ "Nombre" : "Samy" ,
96
+ "Edad" : "19" ,
97
+ "FechaNacimiento" : "10/04/2004" ,
98
+ "Lenguajes" : "Python, C#, Javascript, Java, PHP"
99
+ }
100
+
101
+ fabrica_json = fabrica_de_archivos ("evilpotato04" , "json" , diccionario )
102
+ fabrica_json .crear_archivo ()
103
+ fabrica_json .leer_archivo ()
104
+ fabrica_json .borrar_archivo ()
105
+
106
+ fabrica_xml = fabrica_de_archivos ("evilpotato04" , "xml" , diccionario )
107
+ fabrica_xml .crear_archivo ()
108
+ fabrica_xml .leer_archivo ()
109
+ fabrica_xml .borrar_archivo ()
110
+
111
+ programa ()
0 commit comments