python - 在Python中将所有内容保留在最后一个 '/'的左侧

标签 python strsplit

我想将所有内容保留在 URL 中最后一个“/”左侧。我可以使用以下命令将所有内容保持在右侧:

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

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

最佳答案

最后一个/左边的部分是的第一个元素“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'

关于python - 在Python中将所有内容保留在最后一个 '/'的左侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53327841/

相关文章:

r - 列中唯一单词总数 - R

r - 将一列拆分为多个 R 并在为真时给出逻辑值

regex - 如何使用 strsplit() 访问 R 列表中的元素?

R-使用str_split和unlist创建两列

python - 使用 python netCDF4 将多字符字符串转换为 netcdf

python - matplotlib PdfPages 复合图像

python - Pandas 中有矢量化的 string.format 吗?

python - 狮身人面像 : force rebuild of html, 包括 autodoc

python - 如何将列表传递给@app.route(/<list>)

regex - 在 R 中计算 strsplit 的输出