在Python中,应该如何提取路径中倒数第二个目录名? [英] In Python, how should one extract the second-last directory name in a path?

查看:638
本文介绍了在Python中,应该如何提取路径中倒数第二个目录名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下字符串:

/cvmfs/atlas.cern.ch/repo/sw/ASG/AnalysisTop/2.0.24/RootCore

如何从该字符串中提取"2.0.24"?我不确定如何使用斜杠来分割字符串(以便提取结果列表的倒数第二个元素),而且我不确定这是否是一个好方法.我现在所拥有的是以下内容:

How should I extract the "2.0.24" from this string? I'm not sure how to split the string using the slashes (in order to extract the second last element of the resultant list) and I'm not sure if this would be a good approach. What I have right now is the following:

"/cvmfs/atlas.cern.ch/repo/sw/ASG/AnalysisTop/2.0.24/RootCore".split("/RootCore")[0].split("AnalysisTop/")[1]

推荐答案

'/cvmfs/atlas.cern.ch/repo/sw/ASG/AnalysisTop/2.0.24/RootCore'.split('/')[-2]

这篇关于在Python中,应该如何提取路径中倒数第二个目录名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆