如何在python字典中获取第一行和下一行的键

  • 本文关键字:一行 python 获取 字典 python
  • 更新时间 :
  • 英文 :


我需要一些帮助来缩小我的问题。

这里是问题,我有一个CSV文件,我已经转换成字典。

csv格式
switch1,10.222.197.8/29,255.255.255.248,ab14-host_mini,100,a05-02,aad
ab14-app2-1-aad.pps.ddc.net,10.222.197.10,255.255.255.248,ab14-host_mini,100,a05-02,aad
ab14-app2-3-aad.pps.ddc.net,10.222.197.11,255.255.255.248,ab14-host_mini,100,a05-02,aad
ab14-cbatch2-1-aad.pps.ddc.net,10.222.197.12,255.255.255.248,ab14-host_mini,100,a05-02,aad
switch2,10.222.197.24/29,255.255.255.248,ab14-host_mini,100,b05-02,aad
vl100--asw1-b05-02-aad.net.ddc.net,10.222.197.25,255.255.255.248,ab14-host_mini,100,b05-02,aad
ab14-app2-4-aad.pps.ddc.net,10.222.197.26,255.255.255.248,ab14-host_mini,100,b05-02,aad
ab14-app2-5-aad.pps.ddc.net,10.222.197.27,255.255.255.248,ab14-host_mini,100,b05-02,aad
switch3,10.222.197.0/29,255.255.255.248,ab14-host_mini,100,b09-02,aad
vl100--asw1-b09-02-aad.net.ddc.net,10.222.197.1,255.255.255.248,ab14-host_mini,100,b09-02,aad
ab14-app1-2-aad.pps.ddc.net,10.222.197.2,255.255.255.248,ab14-host_mini,100,b09-02,aad
ab14-app1-5-aad.pps.ddc.net,10.222.197.3,255.255.255.248,ab14-host_mini,100,b09-02,aad
ab14-cbatch1-1-aad.pps.ddc.net,10.222.197.4,255.255.255.248,ab14-host_mini,100,b09-02,aad
switch4,10.222.197.32/29,255.255.255.248,ab14-host_mini,100,b14-02,aad
vl100--asw1-b14-02-aad.net.ddc.net,10.222.197.33,255.255.255.248,ab14-host_mini,100,b14-02,aad
ab14-app2-2-aad.pps.ddc.net,10.222.197.34,255.255.255.248,ab14-host_mini,100,b14-02,aad
ab14-cbatch2-2-aad.pps.ddc.net,10.222.197.35,255.255.255.248,ab14-host_mini,100,b14-02,aad
switch5,10.222.197.40/29,255.255.255.248,ab14-host_mini,100,c12-02,aad
vl100--asw1-c12-02-aad.net.ddc.net,10.222.197.41,255.255.255.248,ab14-host_mini,100,c12-02,aad
ab14-app1-1-aad.pps.ddc.net,10.222.197.42,255.255.255.248,ab14-host_mini,100,c12-02,aad
ab14-dapp1-1-aad.pps.ddc.net,10.222.197.43,255.255.255.248,ab14-host_mini,100,c12-02,aad
vl112--asw1-a01-01-aad.net.ddc.net,10.222.250.241,255.255.255.248,aad-fdc,112,a01-01,aad
cs97-fdc2-20-aad.pps.ddc.net,10.222.250.242,255.255.255.248,aad-fdc,112,a01-01,aad
cs97-fdc2-22-aad.pps.ddc.net,10.222.250.243,255.255.255.248,aad-fdc,112,a01-01,aad
switch6,10.222.162.32/27,255.255.255.224,aad-fdc,101,a02-01,aad
vl101--asw1-a02-01-aad.net.ddc.net,10.222.162.33,255.255.255.224,aad-fdc,101,a02-01,aad
cs77-fdc2-9-aad.pps.ddc.net,10.222.162.62,255.255.255.224,aad-fdc,101,a02-01,aad
cs92-fdc2-2-aad.pps.ddc.net,10.222.162.34,255.255.255.224,aad-fdc,101,a02-01,aad
cs95-fdc2-2-aad.pps.ddc.net,10.222.162.35,255.255.255.224,aad-fdc,101,a02-01,aad

我使用下面的逻辑将其转换为disdisary。

with open("pam.csv") as f:
reader = csv.reader(f)
mydict = {rows[0]: rows[1] for rows in reader}
print(mydict)

现在我想通过提供IP地址来获取交换机名称之一,我已经编写了逻辑来获取它,但我正在获取服务器名称。

让我们说一个例子:当我在下面的逻辑10.222.197.4中输入时,我试图找到交换机名称,但我得到的服务器名称称为ab14-cbatch1-1- aad.pps.dc.net。

如何获取交换机名称的服务器IP,所以在我的情况下,我应该得到switch3

src = "10.222.197.4"

for key, value in mydict.items():

if src in value:
# key = mydict[src]
print("found")
print(key)

输出:发现ab14-cbatch1-1-aad.pps.ddc.net

只需再添加一个条件来匹配交换机名称模式:

src = "10.222.197.4"
for key, value in mydict.items():
if src in value and "switch" in key:
# key = mydict[src]
print("found")
print(key)

与@Zircoz的基本思想相同,但另一种方法是首先为您的搜索情况创建一个优化的数据结构:

import csv
from pprint import pprint
import re
switch_dict = {}
ip_pattern = r"(d{1,3}.?){4}"
with open("pam.csv") as file:
reader = csv.reader(file)
for row in reader:
if "switch" in row[0]:
ip_address = re.match(ip_pattern, row[1])[0]
switch_dict[ip_address] = row[0]
pprint(switch_dict)

输出:

{'10.222.162.32': 'switch6',
'10.222.197.0': 'switch3',
'10.222.197.24': 'switch2',
'10.222.197.32': 'switch4',
'10.222.197.40': 'switch5',
'10.222.197.8': 'switch1'}

最新更新