在Python中搜索和替换文件内联文本

时间:2010-08-20 03:25:02

标签: python regex replace

我正在尝试将包含传统格式的ip地址的文件转换为包含二进制格式的ip地址的文件。

文件内容如下。

src-ip {192.168.64.54}
dst-ip {192.168.43.87}


我的代码如下。

import re
from decimal import *

filter = open("filter.txt", "r")

output = open("format.txt", "w")

for line in filter:
        bytePattern = "([01]?\d\d?|2[0-4]\d|25[0-5])"
        regObj = re.compile("\.".join([bytePattern]*4))
        for match in regObj.finditer(line):
            m1,m2,m3,m4 = match.groups()
            line = line.replace((' '.join([bin(256 + int(x))[3:] for x in '123.123.123.123'.split('.')])),bytePattern)
            print line

部分line.replace()似乎没有正常工作。行.replace的第一个参数工作正常。(即它将ip地址转换为二进制格式) 但是line.replace似乎不起作用。关于为什么会发生这种情况的任何帮助或线索都值得赞赏。

4 个答案:

答案 0 :(得分:2)

with open('filter.txt') as filter_:
    with open("format.txt", "w") as format: 
        for line in filter_:
            if line != '\n':
                ip = line.split()
                ip[1] = '.'.join(bin(int(x)+256)[3:] for x in ip[1].split('.'))
                ip[4]= '.'.join(bin(int(x)+256)[3:] for x in ip[4].split('.'))
                ip = " ".join(ip) + '\n'
                format.write(ip)

答案 1 :(得分:1)

为什么不利用re.sub()来改善您的替代品并简化您的正则表达式?

import re
from decimal import *

filter = open("filter.txt", "r")

output = open("format.txt", "w")

pattern = re.compile(r'[\d.]+') # Matches any sequence of digits and .'s

def convert_match_to_binary(match)
    octets = match.group(0).split('.')
    # do something here to convert the octets to a string you want to replace
    # this IP with, and store it in new_form
    return new_form

for line in filter:
    line = pattern.sub(convert_match_to_binary, line)
    print line

答案 2 :(得分:0)

你的代码很奇怪:

line = line.replace(
    (' '.join([bin(256 + int(x))[3:] for x in '123.123.123.123'.split('.')])),
    bytePattern
    )

第一个参数是一个常量,其值为'01111011 01111011 01111011 01111011',而bytePattern是正则表达式“([01]?\ d \ d?| 2 [0-4] \ d | 25 [0-5 ])“,所以它实际上是这样的:

line = line.replace('01111011 01111011 01111011 01111011', "([01]?\d\d?|2[0-4]\d|25[0-5])")

如果您的文件中没有01111011 01111011 01111011 01111011,则无法执行任何操作。

.replace()方法只替换文字字符串,而不是正则表达式。

答案 3 :(得分:0)

如果有任何帮助,请参阅DaniWed IP number conversion between dotnumber string and integer的旧代码并添加一些错误检查。

def ipnumber(ip): 
    if ip.count('.') != 3: 
        raise ValueError, 'IP string with wrong number of dots' 
    ip=[int(ipn) for ipn in ip.rstrip().split('.')]
    if any(ipn<0 or ipn>255 for ipn in ip):
        raise ValueError, 'IP part of wrong value: %s' % ip
    ipn=0 
    while ip: 
        ipn=(ipn<<8)+ip.pop(0)
    return ipn 

def ipstring(ip): 
    ips='' 
    for i in range(4): 
        ip,n=divmod(ip,256)
        print n
        if (n<0) or (n>255): 
            raise ValueError, "IP number %i is not valid (%s, %i)." % (ip,ips,n) 
        ips = str(n)+'.'+ips 
    return ips[:-1] ## take out extra point

inp = "src-ip{ 192.168.64.544 } dst-ip{ 192.168.43.87 }"

found=' '
while found:
    _,found,ip = inp.partition('-ip{ ')
    ip,found,inp = ip.partition(' }')
    if ip:
         print ipnumber(ip)