0
# GET NEWS To JSON FILE
def get_news():
    r = requests.get("https://nfs.faireconomy.media/ff_calendar_thisweek.json")
    json_file = r.json()
    # Filter python objects with list comprehensions
    output_dict = [x for x in json_file if (x['impact'] == 'High' or x['impact'] == 'Holiday')]
    # Transform python object back into json
    output_json = json.dumps(output_dict, sort_keys=True, indent=4)
    # Show json
    # print(output_json)
    return output_json

the json file has impact: High , impact: Medium , impact: Low , impact: Holiday.. 4 ways an entry could be in the json file

Now in the code i have only have high and holiday, how can i add low and medium to it while it staying to work

petezurich
  • 9,280
  • 9
  • 43
  • 57
  • What do you mean by "adding" low and medium? Are you referring to the condition in the list comprehension? – BrokenBenchmark Jun 25 '22 at 04:38
  • Or this? https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-for-equality-against-a-single-value – ddejohn Jun 25 '22 at 04:39
  • `output_dict = [x for x in json_file if (x['impact'] == 'High' or x['impact'] == 'Holiday' or x['impact'] == 'Medium' or x['impact'] == 'Low')]` – Alexander Jun 25 '22 at 04:47
  • That's a bad solution @alexpdev -- maybe check out the two posts I linked for something that can actually scale, cuts down on excessive operations, is clearer, and faster? – ddejohn Jun 25 '22 at 04:48
  • @ddejohn Its the solution that requires the least amount of understanding and the least amount of changes to the code. It's obvious that the OP isn't very familiar with python and I didn't think this question warranted a full answer – Alexander Jun 25 '22 at 04:51
  • Interesting strategy, teaching OP bad habits, sending them down a path with a dead end instead of helping them grow as a developer... – ddejohn Jun 25 '22 at 04:58
  • You're right that this question doesn't warrant an answer though -- because it already has the canonical answer documented in dozens of duplicates. – ddejohn Jun 25 '22 at 04:59

1 Answers1

1

You can write like this.

impacts = ['High', 'Low', 'Medium', 'Holiday']
outPut_dicts = [x for x in json_data if (x.get('impact') in impacts)]
Mazhar Ali
  • 111
  • 2
  • 6
  • File "main.py", line 77, in called_once_a_day await message_channel.send(embed=embed) File "/home/runner/Infinity-Forex-News/venv/lib/python3.8/site-packages/discord/abc.py", line 1065, in send data = await state.http.send_message(channel.id, content, tts=tts, embed=embed, File "/home/runner/Infinity-Forex-News/venv/lib/python3.8/site-packages/discord/http.py", line 254, in request raise HTTPException(r, data) discord.errors.HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body In embed: Embed size exceeds maximum size of 6000 – Youssef Gerges Jun 25 '22 at 07:06