How about start from below:
import ipaddress
def process(iptext):
try:
print(ipaddress.ip_interface(iptext).network)
return
except Exception:
print("INVALID")
return
with open('ipaddresses.txt', 'r') as f:
for line in f:
line = "".join(line.split())
process(line)
Your ipaddresses.txt would have IPs like:
192.168.100.12/24
192.168.101.32/24
Looks nice reference related to IP:
Working with IP addresses in Python - ipaddress library - part 1 |[
^]