简体   繁体   English

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

[英]search and replace text inline in file in Python

I am trying to convert a file which contains ip address in the traditional format to a file which contains ip address in the binary format. 我正在尝试将包含传统格式ip地址的文件转换为包含二进制格式ip地址的文件。

The file contents are as follows. 文件内容如下。

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


The code I have is as follows. 我的代码如下。

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

The portion line.replace() does not seem to be working fine. line.replace()部分似乎工作不正常。 The first parameter to line .replace is working fine.(ie it is converting the ip address into the binary format) But line.replace doesn't seem to work. line.replace的第一个参数可以正常工作(即,它将IP地址转换为二进制格式),但是line.replace似乎不起作用。 Any help or clues as to why this happens is appreciated. 任何帮助或线索,为什么会发生这种情况表示赞赏。

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)

Why not take advantage of re.sub() instead, to both make your replacements easier and simplify your regex? 为什么不利用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

Your code is very odd: 您的代码很奇怪:

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

The first argument is a constant that evaluates to '01111011 01111011 01111011 01111011', and bytePattern is the regex "([01]?\\d\\d?|2[0-4]\\d|25[0-5])", so it's effectively this: 第一个参数是一个常数,其值为'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])")

This won't do anything if your file doesn't have 01111011 01111011 01111011 01111011 in it. 如果文件中没有01111011 01111011 01111011 01111011 ,则此操作将无效。

The .replace() method only replaces literal strings, not regexes. .replace()方法仅替换文字字符串,而不替换正则表达式。

If it is any help here is my old code from DaniWed IP number conversion between dotnumber string and integer with some error check added. 如果有帮助,这是我从DaniWed IP数字转换点号字符串和整数之间的旧代码, 添加了一些错误检查。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM