V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
itsjoke
V2EX  ›  Python

初学 Python ,求大佬指导个 Python 思路

  •  1
     
  •   itsjoke · 2017-10-11 11:58:12 +08:00 · 2901 次点击
    这是一个创建于 2360 天前的主题,其中的信息可能已经有所发展或是发生改变。

    有以下文本,大多数情况下都是第一段文本,偶尔会有第二段,相比第一个多了一个ip address x.x.x.x x.x.x.x

    4500#sh run int vlan 3
    Building configuration...
    
    Current configuration : 122 bytes
    !
    interface Vlan3
     ip address 192.168.234.17 255.255.255.240
     no ip redirects
     no ip proxy-arp
    end
    
    4500#sh run int vlan 4
    Building configuration...
    
    Current configuration : 192 bytes
    !
    interface Vlan4
     ip address 192.168.23.65 255.255.255.192 secondary
     ip address 192.168.4.1 255.255.255.0
     ip access-group 100 in
     no ip redirects
     no ip unreachables
     no ip proxy-arp
    end
    

    我用这段正则来匹配:

    vlan_info = vlan_output.encode('ascii','ignore')
    vlan_info_re = re.search(r"(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b) (\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b)",vlan_info)
    gateway = vlan_info_re.group(1)
    netmask = vlan_info_re.group(2)
    

    如果仅是第一段文本的话是可以正常匹配的,但如果第二段也存在的话就只会匹配到第一个。 求大佬指导个 python 的思路,怎么把文本里面的 ip 和掩码全取出来,应该怎么写才比较 pythonic? 谢谢!

    11 条回复    2017-10-12 09:13:00 +08:00
    yunkchen
        1
    yunkchen  
       2017-10-11 13:10:17 +08:00   ❤️ 1
    re.findall(r"\d+\.\d+\.\d+\.\d+", vlan_info)
    superhan
        2
    superhan  
       2017-10-11 13:42:20 +08:00
    正确的思路是找一个库
    itsjoke
        3
    itsjoke  
    OP
       2017-10-11 13:42:51 +08:00
    我是想分别取得网段的值,所以没有用 findall
    一时半会儿没想到应该怎么写了
    前来求助
    glongzh
        4
    glongzh  
       2017-10-11 13:48:01 +08:00   ❤️ 1
    多查手册,re.search 只会返回第一个 Match
    itsjoke
        5
    itsjoke  
    OP
       2017-10-11 14:00:29 +08:00
    @glongzh 这个我知道是只会返回第一个 Match
    我想的是毕竟第二段这种情况较少,难道也只能单独再写一个来匹配吗?
    或者说有什么其他的 pythonic 的方法来实现呢?
    julyclyde
        6
    julyclyde  
       2017-10-11 14:18:24 +08:00
    网络设备的这种配置,其实不适合用这种方法来解析
    itsjoke
        7
    itsjoke  
    OP
       2017-10-11 14:25:19 +08:00
    @julyclyde 应该怎么弄呢?麻烦有空指导下了,多谢。
    ferstar
        8
    ferstar  
       2017-10-11 17:08:27 +08:00
    你这个需求不需要用正则的
    ```python
    import sys


    def main(fp):
    print('ip\tmask')
    with open(fp, 'r') as fh:
    for line in fh:
    line_trim = line.strip()
    if line_trim.startswith('ip address'):
    lst = line_trim.split()
    ip, mask = lst[2], lst[3]
    print('{}\t{}'.format(ip, mask))


    if __name__ == '__main__':
    if len(sys.argv) != 2:
    print("usage: {} <ip_file>".format(sys.argv[0]))
    sys.exit(1)
    main(sys.argv[1])

    ```
    ferstar
        9
    ferstar  
       2017-10-11 17:13:02 +08:00
    @ferstar 我去, v2 的回复真是无力吐槽, 代码见 gist: https://gist.github.com/ferstar/fb9156300827f6eb18aa5e219970cfec
    Glink
        10
    Glink  
       2017-10-11 21:27:47 +08:00   ❤️ 1
    cat file |grep 'ip address'|awk -F ' ' '{print $3" "$4}'
    一句 shell 能解决的问题,非要写 python
    itsjoke
        11
    itsjoke  
    OP
       2017-10-12 09:13:00 +08:00
    @Glink [dog]我这个又并不是解决单个问题,我这个是在 python 下面的一段问题而已。。。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5310 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 09:33 · PVG 17:33 · LAX 02:33 · JFK 05:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.