By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,080 Members | 1,855 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,080 IT Pros & Developers. It's quick & easy.

How do I create a loop for this Python code in excel?

P: 3
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.

Expand|Select|Wrap|Line Numbers
  1. >>> url = ("http://www.example.com")
  2. >>> domain = url.split("http://")[-1].split("/")[0]
  3. >>> print(domain)
  4.     www.example.com
  5. >>> url_2 = ("http://example.com")
  6. >>> domain_2 = url_2.split("http://")[-1].split('/')[0]
  7. >>> print(domain_2)
  8.     example.com
  9. >>> 
  10. >>> #Now to remove www. from the first one
  11. >>> 
  12. >>> domain_without_w3 = domain.split("www.")[-1]
  13. >>> print(domain_without_w3)
  14.     example.com
  15. >>> 
  16. >>> # both the commands would have to beexceuted on all the
  17. >>> # values in a coloumn to extract the domain
  18. >>> # , So here I execute the second
  19. >>> # command too on the second url.
  20. >>> 
  21. >>> domain_2_without_w3 = domain_2.split("www.")[-1]
  22. >>> print(domain_2_without_w3)
  23.     example.com
  24. >>> 
  25. >>> #Ok so how do i do these to commands on a list of urls in a column
  26. >>> # in excel automatically. That's My question.
  27. >>> #I am sorry if anything in here looks stupid or absurd
  28. >>> #I literally learned python 2 days ago.
  29.  
Jun 10 '19 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 1,035
It's unclear what you want
1) do you want to create a loop in python?
2) do you want the Excel equivalent of this code?

Do you want a python solution? or an Excel solution (in which case this post needs to be moved )
Jun 10 '19 #2

Post your reply

Sign in to post your reply or Sign up for a free account.