【问题标题】:Json Scraping, output_dict [duplicate]Json Scraping,output_dict [重复]
【发布时间】:2022-08-15 15:45:03
【问题描述】:
# 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

json文件有影响:高,影响:中,影响:低,影响:假期.. json 文件中的 4 种方式

现在在代码中我只有高和假期,我如何在它继续工作的同时添加低和中

  • “添加”低和中是什么意思?您是指列表理解中的条件吗?
  • output_dict = [x for x in json_file if (x[\'impact\'] == \'High\' or x[\'impact\'] == \'Holiday\' or x[\'impact\'] == \'Medium\' or x[\'impact\'] == \'Low\')]
  • 那是一个糟糕的解决方案@alexpdev——也许看看我链接的两个帖子,这些帖子实际上可以扩展,减少过多的操作,更清晰,更快?
  • @ddejohn 它是需要最少理解和最少更改代码的解决方案。很明显,OP 对 python 不是很熟悉,我认为这个问题不需要完整的答案

标签: python discord


【解决方案1】:

你可以这样写。

impacts = ['High', 'Low', 'Medium', 'Holiday']
outPut_dicts = [x for x in json_data if (x.get('impact') in impacts)]

【讨论】:

  • 文件“main.py”,第 77 行,在 called_once_a_day await message_channel.send(embed=embed) 文件“/home/runner/Infinity-Forex-News/venv/lib/python3.8/site-packages/discord/abc. py",第 1065 行,在发送数据 = 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”,第 254 行,在请求中引发 HTTPException(r,data)discord.errors.HTTPException:400 错误请求(错误代码:50035):嵌入中的表单正文无效:嵌入大小超过最大大小为 6000
猜你喜欢
  • 2016-12-17
  • 2021-09-09
  • 2020-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-03
相关资源
最近更新 更多