【问题标题】:Keeping everything on the left of the last '/' in Python [duplicate]将所有内容保留在 Python 中最后一个“/”的左侧 [重复]
【发布时间】:2019-04-19 01:13:17
【问题描述】:

我想将所有内容保留在 URL 中最后一个“/”的左侧。我可以使用以下方法将所有内容保持在正确的位置:

"https://ideas.repec.org/s/fip/fedgfe.html".rsplit('/', 1).pop()

但是,我想要左边的一切。任何帮助将不胜感激!

【问题讨论】:

    标签: python strsplit


    【解决方案1】:

    最后一个/左边的部分是"https://ideas.repec.org/s/fip/fedgfe.html".rsplit('/', 1)的第一个元素,所以:

    >>> "https://ideas.repec.org/s/fip/fedgfe.html".rsplit('/', 1)[0]
    'https://ideas.repec.org/s/fip'
    

    【讨论】:

      【解决方案2】:
      str="https://ideas.repec.org/s/fip/fedgfe.html"   
      last_forward_slash_index=str.rfind('/')  #rfind() will find the last forward slash '/'
      str1=str[0:last_forward_slash_index]
      print(str1) # It will print "https://ideas.repec.org/s/fip"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-06
        • 2019-02-19
        • 1970-01-01
        • 1970-01-01
        • 2017-11-11
        • 2017-03-01
        • 1970-01-01
        • 2023-02-22
        相关资源
        最近更新 更多