【问题标题】:Extract Json Data with Python使用 Python 提取 Json 数据
【发布时间】:2022-11-21 14:43:52
【问题描述】:

我的 Json 数据如下所示:

    {
    "componentId": "SD1:1100047938",
    "componentType": "Device",
    "name": "WR50MS15-7938 (WR 33)",
    "product": "SB 5000TL",
    "productTagId": 9037,
    "pvPower": 886,
    "serial": "1100047938",
    "specWhOutToday": 3.0909803921568626,
    "specWhOutYesterday": 2.924313725490196,
    "state": 307,
    "totWhOutToday": 15764,
    "totWhOutYesterday": 14914
}

我怎么能只提取:

  • “状态”到一个单独的文件
  • “pv-power”到一个单独的文件?

谢谢 !

【问题讨论】:

  • 你尝试了什么?你知道如何访问 Python 字典中的键和值吗?
  • 您将 json 读入变量并提取。我们需要示例代码来查看哪里有问题。

标签: python json


【解决方案1】:
import requests
import json
import urllib3
import sys
import requests

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

fehler = '"state": 35'
urltoken = "https://172.16.63.100/api/v1/token"
urldaten = "https://172.16.63.100/api/v1/overview/Plant:1/devices?todayDate=2022-10-17T12%3A53%3A16.746Z"
urlaktuell = "https://172.16.63.100/api/v1/widgets/gauge/power?componentId=Plant%3A1&type=PvProduction"

data = {
    "grant_type": "password",
    "username": "...",
    "password": "...",
}

response = requests.post(urltoken, data, verify=False)

#print(response.json())

data = json.loads(response.text)

with open('/usr/lib/nagios/plugins/check_DataManager/token.txt', 'w') as f:
    data = json.dump(data, f, indent = 2)

with open('/usr/lib/nagios/plugins/check_DataManager/token.txt') as json_file:
    data1 = json.load(json_file)

token = data1["access_token"]

payload={}
headers = {'Authorization': 'Bearer ' + token }

response = requests.request("GET", urldaten, headers=headers, data=payload, verify=False)


data = json.loads(response.text)

with open('/usr/lib/nagios/plugins/check_DataManager/info.txt', 'w') as f:
    data = json.dump(data, f, indent = 2)



if fehler in open('/usr/lib/nagios/plugins/check_DataManager/info.txt').read():
    print("Mindestens ein Wechselrichter hat einen Fehler!")
    exit(1)

else:
    print("Alle Wechselrichter Online!")
    exit(0)


payload={}
headers = {'Authorization': 'Bearer ' + token }

response = requests.request("GET", urlaktuell, headers=headers, data=payload, verify=False)

aktuell = json.loads(response.text)

daten = aktuell["value"]

print("Aktuelle Leistung:", 0.001*daten , "KWh")

我现在设法做到了我想做的一切:)

【讨论】:

  • 请考虑在源代码中添加一些解释,解释它是如何解决问题的。
【解决方案2】:
    url = "https://172.16.63.100/api/v1/overview/Plant:1/devices?todayDate=2022-10-17T12%3A53%3A16.746Z"

payload={}
headers = {
  'Authorization': 'Bearer eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2NjYwMTU0MzMsInN1YiI6Ik1iYWNobCIsInVpZCI6Ijc1YmNkNTM2LTFhOTYtNDQ4My05MjQxLWZkNjY5Zjk3M2Y5OCIsImV4cCI6MTY2NjAxOTAzM30.bMMAsD8iPrAXDp7fbnwYL3Y8lj4Ktok3tU9NHZkYq8s'
}

response = requests.request("GET", url, headers=headers, data=payload, verify=False)

#print(response.text)

data = json.loads(response.text)

with open('data.json', 'w') as f:
    data = json.dump(data, f, indent = 2) 

这是我收集 JSON 数据的代码。

我需要提取上述值。

【讨论】:

  • 您好,请使用其他详细信息更新您的问题。如果它解决了问题,则只发布答案。如果你解决了你的问题,你当然可以写一个答案。
【解决方案3】:

运行 data = json.loads(response.text) 后,json 将作为 python 字典加载到 data 变量中。 所以 state = data.statepvPower = data.pvPower 应该会提供您想要的信息。

至于提取到单独的文件,我不确定您想如何处理这些信息。但是,json.dump() 确实将数据输出到 json 文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-24
    • 2017-04-16
    • 2020-05-06
    • 2020-07-13
    • 2020-08-24
    • 1970-01-01
    • 2020-06-02
    • 2021-03-27
    相关资源
    最近更新 更多