2010-08-20 73 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)