0

I dont know how to read (or split) each dictionary of the .txt file

I have a database(list) that have dictionaries. Each of this dictionaries have the caracteristics of a person(name, age , etc...) I have the class person and want to put all this atributes to and objets.

I dont know how to read (or split) each dictionary of the .txt file

this is an example of the database that i have:

[ { "idolos": [], "nombre": "Juan Lopez", "clave": "m6j0NI", "ramos_pre": [], "alumno": "NO", "usuario": "jlopez" }, { "idolos": [], "nombre": "Paulina Toro", "clave": "KaEEkNjFz", "ramos_pre": [], "alumno": "NO", "usuario": "ptoro" }] 

pd: I cant use any library

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136

1 Answers1

0

You can simply use eval:

s = '[ { "idolos": [], "nombre": "Juan Lopez", "clave": "m6j0NI", "ramos_pre": [], "alumno": "NO", "usuario": "jlopez" }, { "idolos": [], "nombre": "Paulina Toro", "clave": "KaEEkNjFz", "ramos_pre": [], "alumno": "NO", "usuario": "ptoro" }]'
d = eval(s)

Then d is a list of dictionaries. You can access each item as usual:

print d[0]

Output:

{'usuario': 'jlopez', 'alumno': 'NO', 'clave': 'm6j0NI', 'idolos': [], 'ramos_pre': [], 'nombre': 'Juan Lopez'}

Edit: If you can use python standard libraries, look into the ast module, as I did in this answer.

Community
  • 1
  • 1
Falko
  • 17,076
  • 13
  • 60
  • 105