亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Python 中比較兩個 Json 數組?

如何在 Python 中比較兩個 Json 數組?

明月笑刀無情 2023-01-04 13:32:23
我正在創建一個腳本,該腳本允許將兩個 ssh 命令輸出與遠程 Netapp 進行比較,并且允許在當前值和 Netapp 機艙具有的最大空間值之間進行比較。我已經在兩個字典(rv 和 rv 2)中收集了這些值,然后我將它們轉換為 JSON 格式,以便能夠根據傳遞給它的警告參數來比較它們(如果超過這個限制,它就會通知)。我想比較的值的示例: RV1:{'node1.storePool_Owner': ['160'], 'node1.storePool_Deleg': ['0'], 'node2.storePool_LockState': ['0']}RV2:{'node1.storePool_Owner': ['1024000'], 'node1.storePool_Deleg': ['1024000'], 'node2.storePool_LockState': ['1024000']}這個想法是將這些值中的每一個與它們的最大等效值進行比較。非常感謝你的幫助。應該如何比較的一個例子: 如果這個節點,具有那個值:node1.storePool_Owner': ['160']達到以下的 X%(警告 arg):node1.storePool_Owner': ['1024000']然后它應該返回:WARNING: node1.storePool_Owner has exceeded the threshold (x%)
查看完整描述

1 回答

?
慕無忌1623718

TA貢獻1744條經驗 獲得超4個贊

這將比較兩個 json 文件/字典。我注意到這些值是字符串列表......這是故意的嗎?


import json



def open_json(path):

    with open(path, 'r') as file:

        return json.load(file)



def check_thresholds(data, thresholds):

    for k, v in thresholds.items():

        # your logic/output here

        # k[0] and v[0] because your values are lists of strings... should they be just int of float?

        difference = int(data.get(k)[0]) - int(v[0])

        if difference >= 0:

            print(f'WARNING: {k} has exceeded the threshold by {difference}')

        else:

            print(f'OK: {k}')



def main():

    # load the data into dictionaries

    data = open_json('./rv.json')

    thresholds = open_json('./rv2.json')


    # if your data is a dictionary and not json files then use these

    # data = {'node1.storePool_Owner': ['160'], 'node1.storePool_Deleg': ['0'], 'node2.storePool_LockState': ['0']}

    # thresholds = {'node1.storePool_Owner': ['1024000'], 'node1.storePool_Deleg': ['1024000'], 'node2.storePool_LockState': ['1024000']}


    # run the checks

    check_thresholds(data, thresholds)



main()


輸出(我修改了一些值以顯示警告):


WARNING: node1.storePool_Owner has exceeded the threshold by 1000

OK: node1.storePool_Deleg

OK: node2.storePool_LockState


查看完整回答
反對 回復 2023-01-04
  • 1 回答
  • 0 關注
  • 159 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號