[英]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.