Add column with domain name to CSV file with Python

A script to add a column containing only the domain name to an existing CSV file. It extract it from a column containing an URL.

It works with .co.uk and other country code top-level domain.

Just change “5” by the column containing the URL.

Also don’t forget to adjust. Here is it setup for semi-colon for input and output. Just change the delimiter by the one you need.

import csv
import tldextract

with open('input.csv','r') as csvinput:
    with open('output.csv', 'w') as csvoutput:
        writer = csv.writer(csvoutput, delimiter=';')
        reader = csv.reader(csvinput, delimiter=';')

        all = []
        row = next(reader)
        row.append('domain_name')
        all.append(row)

        for row in reader:
            #Column of URL is #5
            ext = tldextract.extract(row[5])
            row.append(ext.registered_domain)
            all.append(row)

        writer.writerows(all)

chevron_left
chevron_right

Leave a comment

Your email address will not be published. Required fields are marked *

Comment
Name
Email
Website