簡體   English   中英

有沒有辦法比較 json 中的兩個列表的元素?

[英]Is there a way to compare the elements of two lists residing in a json?

我目前正在從日志中解析消息並將它們打印出來。 在我解釋我的問題之前,請參閱下面的程序。

    count = 0
    error_list = ["*SSH connection as gentan (using password) successful.*", "*Deployment State Updated to ATTEMPTING_CONNECTION*"]
    
    for i in data['deploymentStatus']['page']:
        count = count + 1
        regex = re.compile(error_list)
        if re.findall(regex, i['history']) is True:
            string = """
        ========================
            TARGET # %s
        ========================
        [+] IP      => %s
        [+] HISTORY => %s""" % (count, i['address'], i['history'])
            print(string)

這是i['history']的代碼。

i['history] = ["13/04/2021 05:42:59:589Z: Attempting connection via ssh as gentan.", "13/04/2021 05:42:59:589Z: Deployment State Updated to ATTEMPTING_CONNECTION"]

現在最后我想要的是,我希望 if 語句只打印在error_listi['history']中相互匹配的日志

在我的情況下它不匹配的原因是因為它以日期和時間開頭,python 中有沒有辦法解析出來並只比較字符串?

我將不勝感激任何幫助。 我也可以發布完整的數據['deploymentStatus']['page'] json 文件,但它太長了,無法在此處發布12k+ 行

您不需要使用正則表達式。 只需使用in運算符來測試error_list中的字符串之一是否在歷史字符串中。

error_list = ["SSH connection as gentan (using password) successful", "Deployment State Updated to ATTEMPTING_CONNECTION"]

for i in data['deploymentStatus']['page']:
    if any(error in i['history'] for error in error_list):
        print("""
        ========================
            TARGET # %s
        ========================
        [+] IP      => %s
        [+] HISTORY => %s""" % (count, i['address'], i['history']))

count = len(data['deploymentStatus']['page']) # no need to calculate this in the loop

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM