使用python从文本文件解析IP地址/网络 [英] IP address/network parsing from text file using python

查看:598
本文介绍了使用python从文本文件解析IP地址/网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下文本文件,我需要一些解析IP地址的帮助。

I have the below text file that I would need some help with parsing out IP addresses.

文本文件格式为

abc 10.1.1.1/32   aabbcc
def 11.2.0.0/16   eeffgg
efg 0.0.0.0/0   ddeeff

换句话说,一堆IP网络作为日志文件的一部分存在。输出应如下所示:

In other words, a bunch of IP networks exist as part of a log file. The output should be provided as below:

10.1.1.1/32
11.2.0.0/16
0.0.0.0/0

我有以下代码,但没有输出所需信息

I have the below code but does not output the required information

file = open(filename, 'r')
for eachline in file.readlines():
    ip_regex = re.findall(r'(?:\d{1,3}\.){3}\d{1,3}', eachline)
    print ip_regex


推荐答案

首先,你的正则表达式甚至不会尝试捕获除了四个虚线数字之外的任何东西,所以当然它不会与其他任何东西相匹配,比如最后的 / 32 。如果你只是添加,例如 / \d {1,2} 到最后,它将修复:

First, your regex doesn't even attempt to capture anything but four dotted numbers, so of course it's not going to match anything else, like a /32 on the end. if you just add, e.g., /\d{1,2} to the end, it'll fix that:

(?:\d{1,3}\.){3}\d{1,3}/\d{1,2}

Debuggex演示

但是,如果你不能理解正则表达式,那么你可能不应该理解使用正则表达式作为你永远无法调试或扩展的魔法。使用 str 等方法更加冗长,如 split find ,但对新手来说可能更容易理解:

However, if you don't understand regular expressions well enough to understand that, you probably shouldn't be using a regex as a piece of "magic" that you'll never be able to debug or extend. It's a bit more verbose with str methods like split or find, but maybe easier to understand for a novice:

for line in file:
    for part in line.split()
        try:
            address, network = part.split('/')
            a, b, c, d = address.split('.')
        except ValueError:
            pass # not in the right format
        else:
            # do something with part, or address and network, or whatever






作为附注,根据您对这些事情的实际操作,您可能想要使用 ipaddress 模块(或在PyPI上的backport 2.6-3.2)而不是字符串解析:


As a side note, depending on what you're actually doing with these things, you might want to use the ipaddress module (or the backport on PyPI for 2.6-3.2) rather than string parsing:

>>> import ipaddress
>>> s = '10.1.1.1/32'
>>> a = ipaddress.ip_network('10.1.1.1/32')

您可以将其与以上:

for line in file:
    for part in line.split():
        try:
            a = ipaddress.ip_network(part)
        except ValueError:
            pass # not the right format
        else:
            # do something with a and its nifty methods

这篇关于使用python从文本文件解析IP地址/网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆