【发布时间】: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 不是很熟悉,我认为这个问题不需要完整的答案