Python 如何在某个字符之前获取字符串的最后一部分?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15851568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:10:26  来源:igfitidea点击:

how to get the last part of a string before a certain character?

pythonstringpython-2.7splitslice

提问by user1063287

I am trying to print the last part of a string before a certain character.

我试图在某个字符之前打印字符串的最后一部分。

I'm not quite sure whether to use the string .split() method or string slicing or maybe something else.

我不太确定是否使用字符串 .split() 方法或字符串切片或其他方法。

Here is some code that doesn't work but I think shows the logic:

这是一些不起作用的代码,但我认为显示了逻辑:

x = 'http://test.com/lalala-134'
print x['-':0] # beginning at the end of the string, return everything before '-'

Note that the number at the end will vary in size so I can't set an exact count from the end of the string.

请注意,末尾的数字大小会有所不同,因此我无法从字符串的末尾设置确切的计数。

采纳答案by Martijn Pieters

You are looking for str.rsplit(), with a limit:

您正在寻找str.rsplit(),有限制:

print x.rsplit('-', 1)[0]

.rsplit()searches for the splitting string from the end of input string, and the second argument limits how many times it'll split to just once.

.rsplit()从输入字符串的末尾搜索拆分字符串,第二个参数将拆分的次数限制为一次。

Another option is to use str.rpartition(), which will only ever split just once:

另一种选择是使用str.rpartition(),它只会分裂一次:

print x.rpartition('-')[0]

For splitting just once, str.rpartition()is the faster method as well; if you need to split more than once you can only use str.rsplit().

对于只拆分一次,str.rpartition()也是更快的方法;如果您需要多次拆分,则只能使用str.rsplit().

Demo:

演示:

>>> x = 'http://test.com/lalala-134'
>>> print x.rsplit('-', 1)[0]
http://test.com/lalala
>>> 'something-with-a-lot-of-dashes'.rsplit('-', 1)[0]
'something-with-a-lot-of'

and the same with str.rpartition()

和同样的 str.rpartition()

>>> print x.rpartition('-')[0]
http://test.com/lalala
>>> 'something-with-a-lot-of-dashes'.rpartition('-')[0]
'something-with-a-lot-of'

回答by dvijparekh

Difference between splitand partitionis split returns the list withoutdelimiter and will split where ever it gets delimiter in string i.e.

splitpartition之间的区别是 split 返回没有分隔符的列表,并将在字符串中获取分隔符的地方进行拆分,即

x = 'http://test.com/lalala-134-431'

a,b,c = x.split(-)
print(a)
"http://test.com/lalala"
print(b)
"134"
print(c)
"431"

and partitionwill divide the string with only firstdelimiter and will only return 3 values in list

分区将只用第一个分隔符分割字符串,并且只返回列表中的 3 个值

x = 'http://test.com/lalala-134-431'
a,b,c = x.partition('-')
print(a)
"http://test.com/lalala"
print(b)
"-"
print(c)
"134-431"

so as you want last value you can use rpartitionit works in same way but it will find delimiter from end of string

所以当你想要最后一个值时,你可以使用rpartition它以相同的方式工作,但它会从字符串的末尾找到分隔符

x = 'http://test.com/lalala-134-431'
a,b,c = x.partition('-')
print(a)
"http://test.com/lalala-134"
print(b)
"-"
print(c)
"431"