Jan-19-2022, 09:12 AM
As bowlofred already said: if it's json you should save it as .json and use Python built-in tools for parsing.
However, if you have no control over input data/files then you can use ast.literal_eval() to safely evaluate an expression node or a string containing a Python literal or container display.
However, if you have no control over input data/files then you can use ast.literal_eval() to safely evaluate an expression node or a string containing a Python literal or container display.
from ast import literal_eval with open('literal_values.txt', 'r') as f: data = literal_eval(f.read()) ids = [record['id'] for record in data if record['active']] print(ids)
Output:[5, 25]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.