这是 Python. How to print a certain part of a line after it had been "re.searched" from a file 的跟进
这是初始代码,基于对链接问题的公认答案:
import re
VSP = input("Номер ВСП (четыре цифры): ")
c = re.compile('(\d+\.226\.\S+)\s+\S+' + VSP)
Tunnel0_IP_VSP = re.search(c, open('OUS_IP.txt').read())
print (Tunnel1_IP_VSP.group(1))
Номер ВСП (четыре цифры): 1020
10.226.27.60
我能够在文本文件中找到请求的 IP 地址。
我的目标是以某种方式将字符串 "10.226.27.60" 转换为允许我在数学公式中使用它的格式。例如,我想通过从原始地址的最后一个八位字节中减去 1 来获得另一个地址 "10.226.27.59"。
请您参考如下方法:
您可以使用 Python 3 的 ipaddress 模块:
>>> import ipaddress
>>> ip = ipaddress.IPv4Address('10.226.27.60')
>>> ip - 1
IPv4Address('10.226.27.59')
IPv4Address 类允许算术运算符
文档:https://docs.python.org/3/library/ipaddress.html#operators
