Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
displaying empty list
#6
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.


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]
bowlofred likes this post
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.
Reply


Messages In This Thread
displaying empty list - by vlearner - Jan-19-2022, 05:41 AM
RE: displaying empty list - by bowlofred - Jan-19-2022, 05:55 AM
RE: displaying empty list - by vlearner - Jan-19-2022, 07:32 AM
RE: displaying empty list - by bowlofred - Jan-19-2022, 07:42 AM
RE: displaying empty list - by vlearner - Jan-19-2022, 07:49 AM
RE: displaying empty list - by perfringo - Jan-19-2022, 09:12 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Code with empty list not executing adeana 9 3,801 Dec-11-2023, 08:27 AM
Last Post: buran
  set.difference of two list gives empty result wardancer84 4 1,540 Jun-14-2022, 01:36 PM
Last Post: wardancer84
  Remove empty keys in a python list python_student 7 3,107 Jan-12-2022, 10:23 PM
Last Post: python_student
  What is the value after JOINING an empty list? JaneTan 2 5,276 Jan-04-2021, 06:25 PM
Last Post: deanhystad
  Printing empty list? hhydration 2 2,150 Oct-28-2020, 11:34 AM
Last Post: Atekka
  Stop a function if the list it needs is empty Pedroski55 2 2,959 Jul-25-2020, 11:50 PM
Last Post: Pedroski55
  Need help to make an empty list with possibility to add Arnsol 1 1,838 Mar-19-2020, 06:08 PM
Last Post: michael1789
  append list to empty array SchroedingersLion 1 2,209 Feb-02-2020, 05:29 PM
Last Post: SchroedingersLion
  I created a function that generate a list but the list is empty in a new .py file mrhopeedu 2 2,337 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu
  Inspect.getmembers with isclass returns an empty list Aldar 1 2,813 Oct-02-2019, 01:48 PM
Last Post: Aldar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020