如何在 Python 中拆分两个哈希以获得所需的输出

How do I split between two hashes for desired output in Python

首先,我需要此文件中所有唯一 IP 的列表。

这是我在 python 中读取的文件的一部分:

[node1] - 190.223.252.106 - 用户登录成功

[node2] - 239.84.157.20 - 用户头像上传成功

[node2] - 87.130.185.37 - 用户登录成功

[node6] - 210.155.211.219 - 用户支付成功

[node5] - 64.103.198.103 - 用户登录成功

我的代码:

def UniqueIP(fileparm):
counter = 0
input_file = open(fileparm, 'r')
file_contents = input_file.read()
input_file.close()
ip_list = file_contents.split()
unique_ip = set(ip_list)
for ip in unique_ip:
    counter += 1
    print(str(counter) + ': ' + str(ip) + "\n")

我有一个良好的开端,但我的输出如下所示。我主要获取 IP,但有时也会随机获取其余内容。我只是希望能够拆分“-”并仅获取 IP 作为输出。

29: 191.219.189.162

30: [node3]

31: 21.147.6.59

32: 55.160.104.8

如果行总是相同的,在ip地址前后都有一个-,在那个位置,那么你可以使用split和一个特定的字符,select适当的元素,然后 strip 删除多余的空格

x = "node1] - 190.223.252.106 - User Successful Login"
x.split('-')[1].strip()
# 190.223.252.106

但是,如果有更多变化,您最好使用正则表达式来专门匹配 IP 地址。

您需要遍历每一行:

unique_ips = set()
with open("path/to/file", "r", encoding="utf-8") as file:
  for line in file:
    line_parts = line.split("-", maxsplit=2)
    if len(line_parts) > 2:
      ip = line_parts[1]
      # Maybe you'd want to check if it's an IP here
      # if is_ip(ip):
      unique_ips.add(ip)

然后你可以遍历集合

for index, ip in enumerate(unique_ips):
  print(f"{index+1}: {ip}")

在将 IP 添加到集合之前,我还会验证它实际上是一个 IP - 它恰好有 4 个字节(介于 0 和 255 之间),由点分隔:

def is_ip(some_str):
  try:
    bvalues = list(map(int, some_str.split(".")))
  except ValueError:
    # Some of the stuff couldn't be parsed into int
    return False
  return all(0<=val<=255 for val in bvalues) and len(bvalues) == 4

(确保在其余代码之前声明此函数)