[英]Python - How to compare the content of two list are in a string
我需要比較列表“present”的所有項目都在字符串“line”中,並且列表“absent”的所有元素都不在字符串“line”中
因此,擁有 2 個列表
present = ['SYN', 'ACK']
absent = ['RST', 'FIN']
還有一個包含所有 TCP 標志的文件: https://github.com/robcowart/elastiflow/blob/master/logstash/elastiflow/dictionaries/tcp_flags.yml
"...
"12": RST-PSH
"13": FIN-RST-PSH
"14": SYN-RST-PSH
"15": FIN-SYN-RST-PSH
"16": ACK
"17": FIN-ACK
"18": SYN-ACK
"19": FIN-SYN-ACK
"20": RST-ACK
"21": FIN-RST-ACK
"22": SYN-RST-ACK
"23": FIN-SYN-RST-ACK
..."
如果“present”的所有元素都存在於該行中並且“absent”的所有元素都不存在於該行中,我將逐行讀取文件,然后打印該行
我該怎么做? 我想象遞歸或理解,但我找不到方法。 謝謝
for line in csv_reader:
# parse the line and store the flags into a list
# flags = line.split...
# the logic to check for present and absent
is_present = all(elem in flags for elem in present)
is_absent = not any(elem in flags for elem in absent)
if is_present and is_absent:
print(line)
line="abcee"
present=['abc','cde','fgh']
absent=['bla','ghj']
def AllInLine():
for i in present:
if i not in line:
return False;
return True;
def NoneInLine():
for i in absent:
if i in line:
return False;
return True;
然后如果兩個函數都返回 true 你可以打印該行
這是您可以嘗試的方法。 它使用requests
從 GitHub 下載原始 YAML 文件,使用PyYAML
解析 YAML ,然后使用all()
檢查每個項目是否absent
和present
線。
YAML 文件也以塊的形式下載,以防它變大。 無論如何,當通過 HTTP 下載文件時,這是一個很好的做法。
演示:
from pathlib import Path
from requests import get
from yaml import safe_load, YAMLError
def download_file(url, chunk_size=1024):
with get(url, stream=True) as req:
filename = Path(url).name
with open(filename, mode="wb") as f:
for chunk in req.iter_content(chunk_size=chunk_size):
if chunk:
f.write(chunk)
def parse_yaml_file(path):
with open(path) as f:
try:
yaml_file = safe_load(f)
return yaml_file
except YAMLError as ex:
print(ex)
if __name__ == "__main__":
present = ['SYN', 'ACK']
absent = ['RST', 'FIN']
yaml_file = download_file("https://raw.githubusercontent.com/robcowart/elastiflow/master/logstash/elastiflow/dictionaries/tcp_flags.yml")
data = parse_yaml_file(yaml_file)
for number, line in data.items():
if all(p in line for p in present) and all(a not in line for a in absent):
print(f"{number}: {line}")
Output:
18: SYN-ACK
26: SYN-PSH-ACK
50: SYN-ACK-URG
58: SYN-PSH-ACK-URG
82: SYN-ACK-ECE
90: SYN-PSH-ACK-ECE
114: SYN-ACK-URG-ECE
122: SYN-PSH-ACK-URG-ECE
146: SYN-ACK-CWR
154: SYN-PSH-ACK-CWR
178: SYN-ACK-URG-CWR
186: SYN-PSH-ACK-URG-CWR
210: SYN-ACK-ECE-CWR
218: SYN-PSH-ACK-ECE-CWR
242: SYN-ACK-URG-ECE-CWR
250: SYN-PSH-ACK-URG-ECE-CWR
注意:上面可能需要對您的場景進行更多的錯誤檢查,但它顯示了總體思路。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.