【问题标题】:How to save json object variable based on specific value如何根据特定值保存json对象变量
【发布时间】:2022-12-24 22:44:24
【问题描述】:

我正在尝试从 python 中的 url 读取 json 响应。下面的代码工作正常但问题是我需要根据主题获取密钥说如果学科是 ”指数每日水平" 那么它应该打印以下内容钥匙 hkr1omlsnteodhkvnt98q20682ghv1fmegb8de01

import json, pandas as pd
import urllib

URL = "https://pv-ft-marketdata-store.ihsmvals-dev.com/email/search"
response = urllib.request.urlopen(URL)
text = response.read()
json_data = json.loads(text)
print(json_data)

【问题讨论】:

  • next(d["key"] for d in json_data if d["subject"] == "Indices Daily level")
  • 嗨,奥尔文,感谢您的建议,我可以知道这里是什么吗?
  • 获取停止迭代错误
  • print(next(d["key"] for d in json_data if d["subject"] == "Indices Daily Level"))
  • json_data.sort(key=itemgetter("processed"))之前排序。不要忘记导入itemgetter()

标签: python json pandas dataframe


【解决方案1】:

要从列表中获取符合某些条件的第一个值,我们可以传递 generator expression,它使用条件直接迭代此列表到 next(),它将从传递的生成器返回第一个值。正如您在 this 评论中提到的,如果有两个或多个值符合条件,您需要获得一个具有“最近处理时间”我假设存储在列表中每个 JSON 对象的 "processed" 键中,并包含 ISO 格式的日期。为此,我们可以对列表进行排序(使用list.sort()"processed"键的值降序排列(将itemgetter()作为key参数传递)查找之前。最后你有 mentioned,你需要在下一个 URL 中使用提取的 "key",所以你只需要将它连接在你提供的两个 URL 路径部分之间。

代码:

import json
from urllib.request import urlopen
from operator import itemgetter

with urlopen('https://pv-ft-marketdata-store.ihsmvals-dev.com/email/search') as resp:
    json_data = json.load(resp)
    
json_data.sort(key=itemgetter("processed"), reverse=True)
key = next(d["key"] for d in json_data if d["subject"] == "Indices Daily Level")

URL = "https://pv-ft-marketdata-store.ihsmvals-dev.com/email/" + key + "/data/html"
print(URL)

【讨论】:

  • 现在我将尝试从第二个 URL 获取 html 表并使用 bs4 import beautifulsoup 和 lxml 将整个表存储在数据框中
  • @RahulVaidya,希望你能成功。
【解决方案2】:
print(next(d["key"] for d in json_data if d["subject"] == "Indices Daily Level"))
key = str(next(d["key"] for d in json_data if d["subject"] == "Indices Daily Level"))

URL = "https://pv-ft-marketdata-store.ihsmvals-dev.com/email/" + 'key'  "/data/html"
print(URL)

【讨论】:

  • 没有在第二个 URL 中获取关键变量
  • 再次阅读this评论。然后再次。重复直到理解到来。
  • 顺便说一句,您不需要将 next() 的返回转换为 str,因为它已经是字符串,只需删除 str() 调用即可。如果你想打印key,只需在给变量赋值后调用print(key),不需要调用next()两次。
  • 仍在试图说服我的想法
  • 嗨,奥尔文,如果我有一个动态主题行,例如“2022-07-20 的 MS 策略每日级别”,我是否可以只给出主题字符串直到“MS 策略每日级别”,它会获取其相应的密钥?
【解决方案3】:
import json
from urllib.request import urlopen
from operator import itemgetter
import pandas as pd
import requests
import re

with urlopen('https://pv-ft-marketdata-store.ihsmvals-dev.com/email/search') as resp:
    json_data = json.load(resp)

json_data.sort(key=itemgetter("processed"), reverse=True)
key = next(d["key"] for d in json_data
if d["subject"].startswith ("Morgan Stanley Systematic Strategies Daily Levels")

URL = "https://pv-ft-marketdata-store.ihsmvals-dev.com/email/" + key + "/data/html"
html = requests.get(URL).content
df_list = pd.read_html(html)
df = df_list[-1]
print(df)

【讨论】:

    猜你喜欢
    • 2020-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-28
    • 2017-07-19
    • 1970-01-01
    相关资源
    最近更新 更多