Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ipaddress.IPv4Network
#11
Here is my second version of it
import ipaddress
import csv

   
def process_file(in_file, out_file):
    with open(in_file, "r", encoding="utf-8") as fin, open(out_file, "w", newline='') as fout:
        rdr = csv.DictReader(fin, delimiter='\t')
        wrtr = csv.writer(fout)
        for row_index, line in enumerate(rdr, start=1):
            ip = line['ipb_address']
            try:          
                net = ((ip_address, ) for ip_address in ipaddress.IPv4Network(ip))
            except ipaddress.AddressValueError:
                print('{} is not valid ip'.format(ip))
            else:
                 wrtr.writerows(net)
    return row_index

if __name__ == '__main__':
    in_file = "fr_block.txt"
    out_file = "fr_block_2.txt"
    num_rows = process_file(in_file=in_file, out_file=out_file)
    print('\n\nTotal rows processed (excluding the header row): {}'.format(num_rows))
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
#12
Buran, thank you for all your help. I am sorry for the late reply. I am assuming that since you got the script to run all the way to the end, something is wrong with my CPU. This script is not processing all the data to the end. I've been having a lot of problems with my computer. Thanks again. Tom
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help with ipaddress package jcrubaugh45 5 1,473 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