在Python中将所有内容保留在最后一个“/”的左边

2024-05-15 05:19:49 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

但是,我希望一切都在左边。任何帮助都将不胜感激!你知道吗


Tags: 方法httpsorgurl内容htmlpopfip
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"

最后/左边的部分是"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'

相关问题 更多 >

    热门问题