Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
displaying empty list
#1
I want to fetch all the respective ID's from the given text file but it is giving me empty list. kindly help me in this.

with open('json-data.txt') as f:
    content = f.read()
    #print(content)

    runners = []

    for index in range(len(content)):
        for key in content[index]:
            if key == 'active' and content[index][key] == True:
                runners.append(content[index]['id'])

    print(runners)
Output:
[]

Attached Files

.txt   json-data.txt (Size: 582 bytes / Downloads: 162)
Reply
#2
Where is this data from? It mentions json, but it's not in json format. It's a python expression, so you could just eval() it, but if you don't control the data, that's not a safe thing to do.

Do you need to parse everything? If you just need the ID numbers, a regular expression is sufficient.

import re

text = open("json-data.txt").read()

print(re.findall(r"'id':\s*(\d+)", text))
Output:
['5', '25', '112']
Reply
#3
If I want to parse the text data here, how should I do it then?
Reply
#4
Why is the file called "json"? Did it start out life as a JSON file? If so, it would be much easier to just read it that way.
ndc85430 likes this post
Reply
#5
(Jan-19-2022, 07:42 AM)bowlofred Wrote: Why is the file called "json"? Did it start out life as a JSON file? If so, it would be much easier to just read it that way.

I am saving the data of the link in the text file that is the reason why I am calling it as 'Json-data'.may be that is the reason why it is
creating ambiguity.
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code with empty list not executing adeana 9 3,636 Dec-11-2023, 08:27 AM
Last Post: buran
  set.difference of two list gives empty result wardancer84 4 1,434 Jun-14-2022, 01:36 PM
Last Post: wardancer84
  Remove empty keys in a python list python_student 7 2,902 Jan-12-2022, 10:23 PM
Last Post: python_student
  What is the value after JOINING an empty list? JaneTan 2 5,062 Jan-04-2021, 06:25 PM
Last Post: deanhystad
  Printing empty list? hhydration 2 2,078 Oct-28-2020, 11:34 AM
Last Post: Atekka
  Stop a function if the list it needs is empty Pedroski55 2 2,867 Jul-25-2020, 11:50 PM
Last Post: Pedroski55
  Need help to make an empty list with possibility to add Arnsol 1 1,774 Mar-19-2020, 06:08 PM
Last Post: michael1789
  append list to empty array SchroedingersLion 1 2,144 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,248 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu
  Inspect.getmembers with isclass returns an empty list Aldar 1 2,743 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