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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 4yush
    New Member
    • Jun 2019
    • 3

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

    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.

    Code:
    >>> 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.
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    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 …)

    Comment

    Working...