第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在 Python 中比較兩個(gè) Json 數(shù)組?

如何在 Python 中比較兩個(gè) Json 數(shù)組?

明月笑刀無(wú)情 2023-01-04 13:32:23
我正在創(chuàng)建一個(gè)腳本,該腳本允許將兩個(gè) ssh 命令輸出與遠(yuǎn)程 Netapp 進(jìn)行比較,并且允許在當(dāng)前值和 Netapp 機(jī)艙具有的最大空間值之間進(jìn)行比較。我已經(jīng)在兩個(gè)字典(rv 和 rv 2)中收集了這些值,然后我將它們轉(zhuǎn)換為 JSON 格式,以便能夠根據(jù)傳遞給它的警告參數(shù)來(lái)比較它們(如果超過(guò)這個(gè)限制,它就會(huì)通知)。我想比較的值的示例: 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']}這個(gè)想法是將這些值中的每一個(gè)與它們的最大等效值進(jìn)行比較。非常感謝你的幫助。應(yīng)該如何比較的一個(gè)例子: 如果這個(gè)節(jié)點(diǎn),具有那個(gè)值:node1.storePool_Owner': ['160']達(dá)到以下的 X%(警告 arg):node1.storePool_Owner': ['1024000']然后它應(yīng)該返回:WARNING: node1.storePool_Owner has exceeded the threshold (x%)
查看完整描述

1 回答

?
慕無(wú)忌1623718

TA貢獻(xiàn)1744條經(jīng)驗(yàn) 獲得超4個(gè)贊

這將比較兩個(gè) 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


查看完整回答
反對(duì) 回復(fù) 2023-01-04
  • 1 回答
  • 0 關(guān)注
  • 166 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)