I need to extract the domain for example: (http: //www.example.com/example-page, http ://test.com/test-page) from a list of websites in an excel sheet and modify that domain to give its url (example.com, test.com). I have got the code part figured put but i still need to get these commands to work on excel sheet cells in a column automatically.

>>> url = ("http://www.example.com")
>>> domain = url.split("http://")[-1].split("/")[0]
>>> print(domain)
    www.example.com
>>> url_2 = ("http://example.com")
>>> domain_2 = url_2.split("http://")[-1].split('/')[0]
>>> print(domain_2)
    example.com
>>> 
>>> #Now to remove www. from the first one
>>> 
>>> domain_without_w3 = domain.split("www.")[-1]
>>> print(domain_without_w3)
    example.com
>>> 
>>> # both the commands would have to beexceuted on all the
>>> # values in a coloumn to extract the domain
>>> # , So here I execute the second
>>> # command too on the second url.
>>> 
>>> domain_2_without_w3 = domain_2.split("www.")[-1]
>>> print(domain_2_without_w3)
    example.com
>>> 
>>> #Ok so how do i do these to commands on a list of urls in a column
>>> # in excel automatically. That's My question.
>>> #I am sorry if anything in here looks stupid or absurd
>>> #I literally learned python 2 days ago.

One little update:

SO i can get it work for excel sheet through this but it doesnt save the changes in the csv sheet automatically

#this is the code
#you first need to install openpyxl

import openpyxl
import os
os.chdir(D:\\pathtomyworkboook)
wb = openpyxl.load_workbook("filename")
wb.get_sheet_names()
#now the shell will tell the sheet names
sheet = wb.get_sheet_by_name('sheetname')
#here's the part where it extracts the domain from url but it 
#can't save it in the sheet itself I have to copy and paste it 
manually

for i in range(2, 90):
     print((sheet.cell(row=i, column=1).value).split
     ('http://')[-1].split('/')[0])

#now after this it will give the sites domain right here
#like this
www.example.com
#etc,.
# after this even if i try
wb.save('example.xlsx')
it doesnt dave the domains shown here but instead keep it a/i
#thats the proble i can create the loop but it doesn't save 
changes to the csv file itself

Can you not simply replace wb.save() to wb.saveas() and specify the proper file ending as CSV instead.
Let the internal workings do what it does best.

Just be ware of naming syntax with / and \ depending on system and so on.

Then again, if you're going to convert it to CSV, why take the step over Excel to produce a plain ASCII text file at all?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.