简体   繁体   English

python在字符串中查找最后一个

[英]python find last in string

I'm looking for a simple method of identifying the last position of a string inside another string ... for instance.我正在寻找一种简单的方法来识别另一个字符串中字符串的最后一个位置......例如。 If I had: file = C:\\Users\\User\\Desktop\\go.py如果我有: file = C:\\Users\\User\\Desktop\\go.py
and I wanted to crop this so that file = go.py我想裁剪这个file = go.py

Normally I would have to run C:\\Users\\User\\Desktop\\go.py through a loop + find statement, and Evey time it encountered a \\ it would ask ... is the the last \\ in the string?通常我必须通过循环 + find 语句运行C:\\Users\\User\\Desktop\\go.py ,并且C:\\Users\\User\\Desktop\\go.py遇到\\它会问......是字符串中的最后一个\\吗? ... Once I found the last \\ I would then file = file[last\\:len(file)] ...一旦我找到了最后一个\\我会然后file = file[last\\:len(file)]
I'm curious to know if there is a faster neater way to do this.. preferably without a loop.我很想知道是否有更快更简洁的方法来做到这一点..最好没有循环。
Something like file = [file('\\', last ):len(file)]类似于 file = [file('\\', last ):len(file)]
If there is nothing like what I've shown above ... then can we place the loop inside the [:] somehow.如果没有像我上面显示的那样......那么我们可以以某种方式将循环放在[:] Something like file = [for i in ...:len(file)]类似于file = [for i in ...:len(file)]

thanks :)谢谢 :)

If it is only about file paths, you can use os.path.basename :如果只是关于文件路径,您可以使用os.path.basename

>>> import os
>>> os.path.basename(file)
'go.py'

Or if you are not running the code on Windows, you have to use ntpath instead of os.path .或者,如果您不在 Windows 上运行代码,则必须使用ntpath而不是os.path

I agree with Felix on that file paths should be handled using os.path.basename .我同意 Felix 的意见,即应该使用os.path.basename处理文件路径。 However, you might want to have a look at the built in string function rpartition .但是,您可能想看看内置的字符串函数rpartition

>>> file = 'C:\Users\User\Desktop\go.py'
>>> before, separator, after = file.rpartition('\\')
>>> before
'C:\\Users\\User\\Desktop'
>>> separator
'\\'
>>> after
'go.py'

There's also the rfind function which gives you the last index of a substring.还有rfind函数,它为您提供子字符串的最后一个索引。

>>> file.rfind('\\')
21

I realize that I'm a bit late to the party, but since this is one of the top results when searching for eg "find last in str python" on Google, I think it might help someone to add this information.我意识到我参加聚会有点晚了,但是由于这是在 Google 上搜索例如“在 str python 中查找最后一个”时的最佳结果之一,我认为它可能有助于某人添加此信息。

You could split the string into a list then get the last index of the list.您可以将字符串拆分为列表,然后获取列表的最后一个索引。

Sample:样本:

>>> file = 'C:\Users\User\Desktop\go.py'
>>> print(file.split('\\')[-1])
go.py

For the general purpose case (as the OP said they like the generalisation of the split solution)...... try the rfind(str) function.对于通用情况(正如 OP 所说,他们喜欢拆分解决方案的泛化)......尝试使用rfind(str)函数。

"myproject-version2.4.5.customext.zip".rfind(".")

edit: apologies, I hadn't realized how old this thread was... :-/编辑:抱歉,我没有意识到这个线程有多老...... :-/

For pathname manipulations you want to be using os.path .对于您想要使用的路径名操作os.path

For this specific problem you want to use the os.path.basename(path) function which will return the last component of a path, or an empty string if the path ends in a slash (ie. the path of a folder rather than a file).对于这个特定问题,您希望使用os.path.basename(path)函数,该函数将返回os.path.basename(path)的最后一个组件,或者如果路径以斜杠结尾(即文件夹的路径而不是一个文件)。

import os.path

print os.path.basename("C:\Users\User\Desktop\go.py")

Gives:给出:

go.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM