Python 使用regex,获取值并创建字典

Python 使用regex,获取值并创建字典,python,regex,dictionary,findall,re,Python,Regex,Dictionary,Findall,Re,第一次发布关于Python的持续学习 目前,我只能做一件非常简单的事情:我有一个路由器的输出——一个称为“接口”的列表——当打印出来时,它是这样的: set interfaces ge-0/0/0 unit 0 family inet address 192.168.15.66/24 set interfaces ge-0/0/1 unit 0 family inet dhcp set interfaces ge-0/0/2 unit 0 family inet address 1.1.1.1/

第一次发布关于Python的持续学习

目前,我只能做一件非常简单的事情:我有一个路由器的输出——一个称为“接口”的列表——当打印出来时,它是这样的:

set interfaces ge-0/0/0 unit 0 family inet address 192.168.15.66/24
set interfaces ge-0/0/1 unit 0 family inet dhcp
set interfaces ge-0/0/2 unit 0 family inet address 1.1.1.1/30
set interfaces ge-0/0/2 unit 0 family inet address 192.168.99.1/30
pattern1 = re.compile(r'ge.{1,7}')
pattern2 = re.compile(r'dhcp')
matched1 = pattern1.findall(interfaces)
matched2 = pattern2.findall(interfaces)
s
e
t

i
n
t
e
r
使用re模块(findall),我正在查找并匹配我需要的一些关键字,如下所示:

set interfaces ge-0/0/0 unit 0 family inet address 192.168.15.66/24
set interfaces ge-0/0/1 unit 0 family inet dhcp
set interfaces ge-0/0/2 unit 0 family inet address 1.1.1.1/30
set interfaces ge-0/0/2 unit 0 family inet address 192.168.99.1/30
pattern1 = re.compile(r'ge.{1,7}')
pattern2 = re.compile(r'dhcp')
matched1 = pattern1.findall(interfaces)
matched2 = pattern2.findall(interfaces)
s
e
t

i
n
t
e
r
此时,问题是:如果此路由器输出的行(?)包含单词“dhcp”,我想保存/存储该值,并获取接口名称/编号。在上面的示例中,包含“dhcp”的接口是“ge-0/0/1”。你们知道我是不是走对了路吗

  • 模型(get two regex)是否有用,然后将这两个值合并到一个字典中,然后使用return打印
  • 我如何解析其余的路由器代码行,以查找这个“dhcp”值,如果没有匹配项,则返回“无”或“不存在”输出
预期输出如下所示(我使用“返回表格”打印输出):

欢迎任何指导。 谢谢


编辑:谢谢你们的回答。我正在添加更多信息,因为可能是关键。 我获得路由器输出的方式是使用SaltStack集成模块。我使用以下语法从Salt Master运行python文件:

interfaces = __salt__['net.cli']('show configuration interfaces | display set', format='xml')['out']['show configuration interfaces | display set']
我真诚地希望输出实际上是一个列表

同时,我使用以下方法进行了另一次尝试:

def dhcp():

interfaces = __salt__['net.cli']('show configuration interfaces | display set', format='xml')['out']['show configuration interfaces | display set']

pattern = re.findall(r'ge.{1,7}', interfaces)
pattern1 = re.findall(r'dhcp', interfaces)

return pattern, pattern1 
结果如下:

outright ~  sudo salt 'vsrx1' red.dhcp
vsrx1:
 |_
   - ge-0/0/0 
   - ge-0/0/0 
   - ge-0/0/0 
   - ge-0/0/0 
   - ge-0/0/1 
   - ge-0/0/2 
   - ge-0/0/2 
 |_
   - dhcp
单独打印列表(使用-->返回(“列表为:“+str(模式)))


如果表达问题的方式在技术上不好,我表示歉意。一般来说,我还在学习这个术语。

我不知道你是如何得到路由器的输出的。但这应该行得通 如果您的数据在此表格中

interface_list = ['set interfaces ge-0/0/0 unit 0 family inet address 192.168.15.66/24',
'set interfaces ge-0/0/1 unit 0 family inet dhcp',
'set interfaces ge-0/0/2 unit 0 family inet address 1.1.1.1/30',
'set interfaces ge-0/0/2 unit 0 family inet address 192.168.99.1/30']
然后

我会给你

{'ge-0/0/0': 'none', 'ge-0/0/1': 'dhcp', 'ge-0/0/2': 'none'}
然后可以将字典更改为数据帧

import pandas as pd

df = pd.Series(interface_dict).to_frame().reset_index().rename(columns={'index':'interface', 0:'service'})

在我看来,你走在正确的轨道上。这是我对你的要求的解释。希望这有帮助。 注意:我是通过手机做的,所以请原谅不正确的缩进

import re

s = ["set interfaces ge-0/0/0 unit 0 family inet address 192.168.15.66/24",
"set interfaces ge-0/0/1 unit 0 family inet dhcp", "set interfaces ge-0/0/2 unit 0 family inet address 1.1.1.1/30", 
"set interfaces ge-0/0/2 unit 0 family inet address 192.168.99.1/30"]

print("interfaces | services")
mydict ={}
def parse_addresses():
    for i in s:
            interface = re.search(r"ge.{1,7}", i)
            if "dhcp" in i:
                service = "dhcp"
                mydict.setdefault("router",{interface.group(): service})
            else:
                service = "None"
            print(f"{interface.group()} | {service}")
    if bool(mydict):
        return mydict
    return "None"

print(parse_addresses())

感谢所有在几个可能的方向上观察和引导我的人。 我在回答我自己的问题(至少90%)。我的最终脚本如下(是的,我知道我的代码可以得到100%的改进,但这是我为实现目标所做的):

def final():

在上述步骤之后,我的输出是:

terminal# sudo salt 'vsrx1' red.final
vsrx1:
|_
  - Sampled interfaces
  - ge-0/0/1 unit 0 
  - ge-0/0/1 unit 0 
|_
  - Not sampled interfaces
  - ge-0/0/0 descrip
  - ge-0/0/0 unit 0 
  - ge-0/0/2 unit 0 
  - ge-0/0/2 unit 0 
|_
  - To enable sampling, insert the following on target device
  - set interfaces Not sampled interfacesfamily inet sampling [input/output]
  - set interfaces ge-0/0/0 descripfamily inet sampling [input/output]
  - set interfaces ge-0/0/0 unit 0 family inet sampling [input/output]
  - set interfaces ge-0/0/2 unit 0 family inet sampling [input/output]
  - set interfaces ge-0/0/2 unit 0 family inet sampling [input/output]
请记住,我正在使用Saltstack代理仆从从Juniper vSRX设备获取信息。我现在需要练习更多的正则表达式来删除所有不必要的输出数据,并对我希望看到的内容进行更精确的处理,就像上面的示例(仍然不清晰),但基本目标已经实现

我在开始时遇到的另一个挑战是理解“interfaces=salt['net.cli']('show configuration interfaces…”的输出,因为我认为每个路由器命令行都是我列表中的一个项/行文件,然后打印出来,打印出来的和我想的不一样:

这是VisualCode打开的文件(名为aaa.txt的文件):

这是由函数打印的文件:

terminal# sudo salt 'vsrx1' red.final
vsrx1:

set interfaces ge-0/0/0 description vsrx1_descr_test
set interfaces ge-0/0/0 unit 0 family inet address 192.168.15.66/24
set interfaces ge-0/0/1 unit 0 family inet sampling input
set interfaces ge-0/0/1 unit 0 family inet sampling output
set interfaces ge-0/0/1 unit 0 family inet dhcp
set interfaces ge-0/0/2 unit 0 family inet address 1.1.1.1/30
set interfaces ge-0/0/2 unit 0 family inet address 192.168.99.1/30
当改变盐的功能时,如:

interfaces = __salt__['net.cli']('show configuration interfaces | display set', format='xml')['out']['show configuration interfaces | display set']"
to this:
interfaces = __salt__['net.cli']('show configuration interfaces | display set', format='xml')['out']
输出是垂直打印的,如下所示:

set interfaces ge-0/0/0 unit 0 family inet address 192.168.15.66/24
set interfaces ge-0/0/1 unit 0 family inet dhcp
set interfaces ge-0/0/2 unit 0 family inet address 1.1.1.1/30
set interfaces ge-0/0/2 unit 0 family inet address 192.168.99.1/30
pattern1 = re.compile(r'ge.{1,7}')
pattern2 = re.compile(r'dhcp')
matched1 = pattern1.findall(interfaces)
matched2 = pattern2.findall(interfaces)
s
e
t

i
n
t
e
r
诸如此类。我的头被那一个打破了

希望这能有所帮助。和往常一样,任何修正都是受欢迎的


克里斯蒂安。

谢谢,@pi_pascal,我会试试看,结果会出来的!谢谢你给我关于熊猫的建议。我把它留了一会儿,想先解决“合并”问题创建我需要的字典。添加了有关路由器输出的信息。谢谢。谢谢,维克托。我添加了更多信息,以避免对信息来源的混淆。希望你对它有另一种观点。
interfaces = __salt__['net.cli']('show configuration interfaces | display set', format='xml')['out']['show configuration interfaces | display set']"
to this:
interfaces = __salt__['net.cli']('show configuration interfaces | display set', format='xml')['out']
s
e
t

i
n
t
e
r