简体   繁体   中英

read (or split) the dictionaries that are in a list in a .txt file

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM