0

So I have a string that contains data below

https://myanimelist.net/animelist/domis1/load.json?status=2&offset=0.

I want to find all 'anime_id' and put them into the list (only numbers). I tried with find('anime_id'), but I can't do this for multiple occurings in the string.

2
  • 3
    Please post your code as well. Commented Jul 2, 2021 at 10:58
  • 1
    Hint: json is a list of dictionaries. Loop through the list entries, and extract the dictionary key (anime_id) Commented Jul 2, 2021 at 11:01

2 Answers 2

1

Here is an example, how to extract anime_id from a json file called test.json, using built-in json module:

import json

with open('test.json') as f:
    data = json.load(f)

# Create generator and search for anime_id
gen = (i['anime_id'] for i in data)

# If needed, iterate over generator and create a list
gen_list = list(gen)

# Print list on console
print(gen_list)
Sign up to request clarification or add additional context in comments.

1 Comment

gen = [...] is not a generator, it's a list. You need to use () instead of [].
0

Your string is in json format, you can parse it with the builtin json module.

import json

data = json.loads(your_string)

for d in data:
    print(d["anime_id"])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.