简体   繁体   English

读取(或拆分).txt文件中的列表中的词典

[英]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 我不知道如何阅读(或拆分).txt文件的每个字典

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 我不知道如何阅读(或拆分).txt文件的每个字典

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 PD:我不能使用任何图书馆

You can simply use eval : 您可以简单地使用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. d是词典的列表。 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 . 编辑:如果可以使用python标准库,请像我在this answer所做的那样查看ast模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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