【问题标题】:Python - Using re to search for a pattern in the value of an imported csv for an if statementPython - 使用 re 在导入的 csv 值中搜索 if 语句的模式
【发布时间】:2023-04-02 22:00:01
【问题描述】:

首先,我很抱歉在这方面是个菜鸟。我有以下代码可以打开一个 CSV 文件并读取它。我试图只返回在名为“源 IP / 详细信息”的字段中包含公共 IP 地址的行以及我知道正在工作的行 [“状态”] 字段。我有我认为是正确的正则表达式,但我不确定我是否正确地进行搜索。另外,我不确定我是否在以下 for 语句中正确设置了变量。

ipRegex = '\b(?!(10)|192\.168|172\.(2[0-9]|1[6-9]|3[0-2]))[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'

with open('whois.csv') as csvDataFile:
csvReader = csv.DictReader(csvDataFile)
rows = [row for row in csvReader if row['Status'] != "Closed" and row['Status'] != "Resolved"] and row['Source IP / Details'] == re.search(ipRegex, row['Source IP / Details'])]

for row in rows:
    case = row['Case Number']
    ipaddr = row['Source IP / Details']

这是我的数据示例:

Case Number,Status,Date/Time Opened,_BATCH_ID_,_BATCH_LAST_RUN_,Alert Source,Alert Subtype,Source IP / Details,
2926,Closed,2015-10-29T11:54:00,2130,2017-10-30T22:48:02,Sophos,[MEDIUM] Alert for Sophos Cloud: A computer does not comply with its Cloud po...,,
7733,Closed,2015-11-18T13:46:00,2130,2017-10-30T22:48:02,Dell SecureWorks,Malicious Network Activity,216.30.178.102,
7818,Closed,2015-11-18T20:58:00,2130,2017-10-30T22:48:02,Dell SecureWorks,Application-Specific Exploits GNU Bash Environment Variable Code Injection attempt(s),,
7850,Closed,2015-11-18T21:47:00,2130,2017-10-30T22:48:02,Dell SecureWorks,Vulnerability Scanning,173.166.95.81,

【问题讨论】:

  • 请提供几行whois.csv 并通过editing your question 更正您的缩进。此外,您的正则表达式与 IP 地址不匹配(请参阅此处:regex101.com/r/uzRthC/1),$ 之后的最后一个 ) 会产生模式错误。
  • 谢谢,我要更正正则表达式。

标签: python regex csv


【解决方案1】:

在查找匹配项时,您需要检查 is not None。此外,如您的 cmets 所述,您的表达有错误。我将最后一行更改为Open 以产生结果:

from io import StringIO
import csv, re

data = """Case Number,Status,Date/Time Opened,_BATCH_ID_,_BATCH_LAST_RUN_,Alert Source,Alert Subtype,Source IP / Details,
2926,Closed,2015-10-29T11:54:00,2130,2017-10-30T22:48:02,Sophos,[MEDIUM] Alert for Sophos Cloud: A computer does not comply with its Cloud po...,,
7733,Closed,2015-11-18T13:46:00,2130,2017-10-30T22:48:02,Dell SecureWorks,Malicious Network Activity,216.30.178.102,
7818,Closed,2015-11-18T20:58:00,2130,2017-10-30T22:48:02,Dell SecureWorks,Application-Specific Exploits GNU Bash Environment Variable Code Injection attempt(s),,
7850,Open,2015-11-18T21:47:00,2130,2017-10-30T22:48:02,Dell SecureWorks,Vulnerability Scanning,173.166.95.81,"""

rx = re.compile(r'^([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(?<!172\.(16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31))(?<!127)(?<!^10)(?<!^0)\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(?<!192\.168)(?<!172\.(16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31))\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(?<!\.255$)$')

with StringIO(data) as csvDataFile:
    csvReader = csv.DictReader(csvDataFile)

    rows = [row for row in csvReader 
            if row['Status'] != 'Closed' and row['Status'] != 'Resolved' 
            and rx.search(row['Source IP / Details']) is not None]
    print(rows)

这会产生

[OrderedDict([('Case Number', '7850'), ('Status', 'Open'), ('Date/Time Opened', '2015-11-18T21:47:00'), ('_BATCH_ID_', '2130'), ('_BATCH_LAST_RUN_', '2017-10-30T22:48:02'), ('Alert Source', 'Dell SecureWorks'), ('Alert Subtype', 'Vulnerability Scanning'), ('Source IP / Details', '173.166.95.81'), ('', '')])]

【讨论】:

  • 这太完美了!非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-06
  • 1970-01-01
  • 2017-09-05
  • 1970-01-01
  • 2012-11-16
  • 2015-09-08
  • 1970-01-01
相关资源
最近更新 更多