Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ipaddress.IPv4Network
#10
The script runs and process file till the end.
As you can see from the screenshot, it process 4786 rows (excluding the header)
   
The last ip is 158.64.55.65 on line 4668 and it's the last one in the output file. The output file is too big to upload it here (some 37 Mb), but you can see the last ip is that one
   

I don't see what else you expect from the script. Here it is with minimal changes
import ipaddress
 
with open("fr_block.txt", "r", encoding="utf8") as fin, open("fr_block_2.txt", "w") as fout:
    next(fin)
    for i, line in enumerate(fin, start=1):
        line = line.split()
        try:          
            net = ipaddress.IPv4Network(line[2])
        except ipaddress.AddressValueError:
            print('{} is not valid ip'.format(line[2]))
            continue
        else:
            for ip_address in net:
                fout.write(str(ip_address) + "\n")
print('\n\nTotal rows processed (excluding the header row): {}'.format(i))
If I have time will add second version, e.g. I would use csv.DictReader to read the inpyt file, etc.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
ipaddress.IPv4Network - by tomthirteen - Dec-17-2018, 08:18 PM
RE: ipaddress.IPv4Network - by buran - Dec-17-2018, 08:24 PM
RE: ipaddress.IPv4Network - by tomthirteen - Dec-17-2018, 08:37 PM
RE: ipaddress.IPv4Network - by buran - Dec-17-2018, 09:00 PM
RE: ipaddress.IPv4Network - by tomthirteen - Dec-17-2018, 09:08 PM
RE: ipaddress.IPv4Network - by buran - Dec-17-2018, 09:27 PM
RE: ipaddress.IPv4Network - by tomthirteen - Dec-17-2018, 09:31 PM
RE: ipaddress.IPv4Network - by buran - Dec-17-2018, 09:33 PM
RE: ipaddress.IPv4Network - by tomthirteen - Dec-17-2018, 09:51 PM
RE: ipaddress.IPv4Network - by buran - Dec-18-2018, 08:01 AM
RE: ipaddress.IPv4Network - by buran - Dec-18-2018, 08:58 AM
RE: ipaddress.IPv4Network - by tomthirteen - Jan-08-2019, 10:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  help with ipaddress package jcrubaugh45 5 2,825 Jul-13-2022, 02:19 PM
Last Post: jcrubaugh45

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020