Dec-18-2018, 08:58 AM
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
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs