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)
✓ Founder of Kelogs, a SaaS SEO Crawler & Log Analyzer
✓ Made for fun the app The Keyword
✓ International SEO Consultant (Freelance)
✓ Over 10 years of SEO experience
✓ Consultant to countless small and big businesses over the decade
Read more about me, myself and I.