Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 分离ip地址的最后八位字节时出现问题_Python - Fatal编程技术网

Python 分离ip地址的最后八位字节时出现问题

Python 分离ip地址的最后八位字节时出现问题,python,Python,我无法从IP地址中拆分最后一个八位字节并将结果打印到文件中 38 IN PTR 10.102.36.38. ( deleted this line ) IN PTR . 11 IN PTR 192.168.100.11. 12 IN PTR 192.168.100.12. 这是电流输出 IN PTR 10.102.36.38. IN PTR . IN PTR 192.168.100.11. IN PTR

我无法从IP地址中拆分最后一个八位字节并将结果打印到文件中

38   IN    PTR   10.102.36.38.
( deleted  this line ) IN    PTR   .
11   IN    PTR   192.168.100.11.
12   IN    PTR   192.168.100.12.
这是电流输出

IN    PTR   10.102.36.38.
IN    PTR   .
IN    PTR   192.168.100.11.
IN    PTR   192.168.100.12.
这是要写入文件的所需输出

38   IN    PTR   10.102.36.38.
( deleted  this line ) IN    PTR   .
11   IN    PTR   192.168.100.11.
12   IN    PTR   192.168.100.12.


类似这样的东西可以达到目的(Python 2和3):

将源数据放在“my.zone”中:

这将产生以下结果(不匹配的行将自动删除):


PTR中的文本“(删除了这一行)是否真的会出现在输出文件中?我收到了一个无效的语法错误?python 1forward_zone.py/etc/puppet/hieradata/v27061603vl0015.yaml文件“1forward_zone.py”,第10行以open(yamlfile)作为infile,open(“/tmp/blocked_ips_test”,“a”)作为outfile:^SyntaxError:无效语法python-V python 2.6.6
import sys
import re

if len(sys.argv) > 1:
    yamlfile = sys.argv[1]
else:
    yamlfile = raw_input("Please enter the yaml server file to  parse, e.g /etc/puppet/hieradata/*.yaml: ")

with open(yamlfile) as infile:
    with open("/tmp/blocked_ips_test", "a") as outfile:
        ips = set()
        for text in infile:
            text = text.rstrip()
            matched = re.search(r'(\d{,3}\.){3}\d{,3}', text)
            if matched:
                ips.add(matched.group())

        for ip in ips:
            line = '%s    IN    PTR   %s.' % (ip.rsplit('.', 1)[-1], ip)
            print line
            print >>outfile, line
import re

r = re.compile(r'^IN\s+PTR\s+([\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.([\d]{1,3})\.)\s*$')

with open("my.zone", "rt") as file:
    for line in file:
        m = r.match(line)
        if m:
            print("{1} IN PTR {0}".format(*m.groups()))
sh$ cat my.zone
IN    PTR   10.102.36.38.
IN    PTR   .
IN    PTR   192.168.100.11.
IN    PTR   192.168.100.12.
38 IN PTR 10.102.36.38.
11 IN PTR 192.168.100.11.
12 IN PTR 192.168.100.12.