2010-08-20 102 views
0

我想将包含传统格式的IP地址的文件转换为包含二进制格式的IP地址的文件。在Python中搜索并替换文本内联文本

文件内容如下。

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()似乎没有工作正常。 line.replace的第一个参数工作正常(即将IP地址转换为二进制格式) 但line.replace似乎不起作用。任何帮助或线索,为什么会发生这种情况是值得赞赏的。

回答

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()方法只替换文字字符串,而不是正则表达式。

+0

他想将原始格式转换成二进制格式地址的文件。 – 2010-08-20 03:33:54

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
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) 
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)