首页 > 代码库 > Python 从文件中筛选出ip 正则表达(re)

Python 从文件中筛选出ip 正则表达(re)



#!/usr/bin/env python
#_*_coding:utf-8 _*_
__author__ = ‘gaogd‘
import  re

with open(‘arr.txt‘,‘r‘) as f:
    for line in f.readlines():
        result2 = re.findall(‘[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}‘, line)
        if not result2  == []:
            print result2[0]
            result = result2[0] + ‘\n‘
            with open(‘arr_ip.txt‘, ‘a+‘) as w:
                w.write(result)


原文件

Array
(
    [2016-08-12] => Array
        (
            [DATE] => 2016-08-12
            [IPS] => Array
                (
                    [0] => 10.26.31.151
                    [1] => 140.206.255.42
                    [2] => 223.104.5.206
                    [3] => 112.65.191.177
                    [4] => 115.209.105.152
                )
        )
    [2016-08-13] => Array
        (
            [DATE] => 2016-08-13
            [IPS] => Array
                (
                )
        )
    [2016-08-14] => Array
        (
            [DATE] => 2016-08-14
            [IPS] => Array
                (
                    [0] => 211.161.245.47
                )
        )
    [2016-08-15] => Array
        (
            [DATE] => 2016-08-15
            [IPS] => Array
                (
                    [0] => 10.26.31.151
                    [1] => 223.104.5.205
                    [2] => 123.245.20.146
                    [3] => 223.104.5.194
                    [4] => 182.86.147.238
                )
        )
    [2016-08-16] => Array
        (
            [DATE] => 2016-08-16
            [IPS] => Array
                (
                    [0] => 10.26.31.151
                )
        )
    [2016-08-17] => Array
        (
            [DATE] => 2016-08-17
            [IPS] => Array
                (
                    [0] => 10.26.31.151
                    [1] => 223.104.5.234
                )
        )
    [2016-08-18] => Array
        (
            [DATE] => 2016-08-18
            [IPS] => Array
                (
                    [0] => 10.26.31.151
                    [1] => 223.104.175.108
                    [2] => 123.184.233.89
                    [3] => 182.200.62.255
                    [4] => 223.104.5.201
                )
        )
    [2016-08-19] => Array
        (
            [DATE] => 2016-08-19
            [IPS] => Array
                (
                    [0] => 10.26.31.151
                    [1] => 223.104.5.235
                    [2] => 223.104.175.65
                )
        )
    [2016-08-20] => Array
        (
            [DATE] => 2016-08-20
            [IPS] => Array
                (
                    [0] => 223.104.5.201
                )
        )
    [2016-08-21] => Array
        (
            [DATE] => 2016-08-21
            [IPS] => Array
                (
                )
        )
    [2016-08-22] => Array
        (
            [DATE] => 2016-08-22
            [IPS] => Array
                (
                    [0] => 10.26.31.151
                    [1] => 112.65.189.136
                    [2] => 218.87.170.214
                )
        )
    [2016-08-23] => Array
        (
            [DATE] => 2016-08-23
            [IPS] => Array
                (
                    [0] => 10.26.31.151
                )
        )
    [2016-08-24] => Array
        (
            [DATE] => 2016-08-24
            [IPS] => Array
                (
                    [0] => 140.206.88.69
                    [1] => 10.26.31.151
                    [2] => 210.13.91.179
                    [3] => 223.104.5.226
                )
        )
    [2016-08-25] => Array
        (
            [DATE] => 2016-08-25
            [IPS] => Array
                (
                    [0] => 10.26.31.151
                    [1] => 180.171.102.48
                )
        )
    [2016-08-26] => Array
        (
            [DATE] => 2016-08-26
            [IPS] => Array
                (
                    [0] => 180.171.102.48
                    [1] => 10.26.31.151
                    [2] => 223.104.5.238
                    [3] => 140.206.255.11
                )
        )
    [2016-08-27] => Array
        (
            [DATE] => 2016-08-27
            [IPS] => Array
                (
                    [0] => 180.171.102.48
                    [1] => 118.133.213.58
                    [2] => 140.207.23.150
                )
        )
    [2016-08-28] => Array
        (
            [DATE] => 2016-08-28
            [IPS] => Array
                (
                    [0] => 118.133.213.58
                    [1] => 140.206.89.196
                    [2] => 180.171.102.48
                )
        )
    [2016-08-29] => Array
        (
            [DATE] => 2016-08-29
            [IPS] => Array
                (
                    [0] => 10.26.31.151
                    [1] => 106.121.3.154
                    [2] => 182.86.195.46
                )
        )
    [2016-08-30] => Array
        (
            [DATE] => 2016-08-30
            [IPS] => Array
                (
                    [0] => 10.26.31.151
                    [1] => 58.247.55.122
                    [2] => 140.206.89.91
                    [3] => 116.235.22.234
                    [4] => 140.207.21.190
                    [5] => 180.171.100.197
                )
        )
    [2016-08-31] => Array
        (
            [DATE] => 2016-08-31
            [IPS] => Array
                (
                    [0] => 10.26.31.151
                    [1] => 116.228.29.218
                    [2] => 180.171.68.2
                )
        )
    [2016-09-01] => Array
        (
            [DATE] => 2016-09-01
            [IPS] => Array
                (
                    [0] => 10.26.31.151
                    [1] => 112.64.189.141
                    [2] => 180.171.68.2
                    [3] => 223.104.5.197
                )
        )
    [2016-09-02] => Array
        (
            [DATE] => 2016-09-02
            [IPS] => Array
                (
                    [0] => 10.26.31.151
                )
        )
    [2016-09-03] => Array
        (
            [DATE] => 2016-09-03
            [IPS] => Array
                (
                )
        )
    [2016-09-04] => Array
        (
            [DATE] => 2016-09-04
            [IPS] => Array
                (
                    [0] => 114.91.48.125
                )
        )
    [2016-09-05] => Array
        (
            [DATE] => 2016-09-05
            [IPS] => Array
                (
                    [0] => 10.26.31.151
                    [1] => 59.63.13.36
                    [2] => 223.104.5.196
                    [3] => 114.91.48.125
                )
        )
)


本文出自 “奋斗吧” 博客,转载请与作者联系!

Python 从文件中筛选出ip 正则表达(re)