简体   繁体   English

Python - 从列表中替换 IP 中的最后一个八位字节

[英]Python - Replace last Octet in IP Address from list

I have a list of IP addresses where I want to replace the last octet of an IP Address with 0/24我有一个 IP 地址列表,我想用0/24替换 IP 地址的最后一个八位字节

So a list of:所以一个列表:

192.168.1.15
178.155.1.87 
5.26.95.136

Would become a list of:将成为以下列表:

192.168.1.0/24
178.155.1.0/24
5.26.95.0/24
cidr_list = []
for host in sorted_list:
    host = host[:host.rfind('.')+1] + '0/24'
    cidr_list.append(host)

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

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