File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed
Roadmap/12 - JSON Y XML/python Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ *JSON y XML
3+ """
4+ import json
5+ from xml .dom .minidom import Element
6+ import xml .etree .ElementTree as xml
7+ from os import remove
8+
9+ #!Json
10+ #?persona es un objeto de python (diccionario)
11+ persona = {
12+ "nombre" : "sorubadguy" ,
13+ "edad" : "30" ,
14+ "f_nac" : "27/03/94" ,
15+ "leng_progra" : "python"
16+ }
17+
18+ #?convierto a persona en un JSON
19+ def crear_json (persona ):
20+ with open ("archivo.json" , "w" ) as j :
21+ json .dump (persona , j )
22+
23+ #?leer y mostrar el archivo.json
24+ with open ("archivo.json" , "r" ) as j :
25+ #?Leo el archivo Json
26+ objson = json .load (j )
27+
28+ print (objson )
29+ print (type (objson ))
30+
31+ remove ("archivo.json" )
32+
33+ #!XML
34+
35+ def crear_xml (persona ):
36+ pers = xml .Element ("informacion" )
37+ nombre = xml .SubElement (pers , "nombre" ).text = persona ["nombre" ]
38+ edad = xml .SubElement (pers , "edad" ).text = persona ["edad" ]
39+ nacimiento = xml .SubElement (pers , "fecha_nacimiento" ).text = persona ["f_nac" ]
40+ progra = xml .SubElement (pers , "Lenguaje_programacion" ).text = persona ["leng_progra" ]
41+
42+ xml .dump (pers )
43+ datos = xml .ElementTree (pers )
44+ datos .write ("persona.xml" )
45+
46+ crear_xml (persona )
47+
48+ with open ("persona.xml" , "r" ) as datos_xml :
49+ print (datos_xml .read ())
50+ remove ("persona.xml" )
You can’t perform that action at this time.
0 commit comments