我有一根绳子。如何删除某个字符后的所有文本?(在这种情况下……) 之后的文字将…这就是为什么我想删除某一个字符之后的所有字符。


当前回答

这是在python 3.7工作到我 在我的情况下,我需要删除后点在我的字符串可变费用

费用= 45.05 Split_string = fees.split(".", 1)

[0] . Substring = split_string

打印(字符串)

其他回答

import re
test = "This is a test...we should not be able to see this"
res = re.sub(r'\.\.\..*',"",test)
print(res)

输出:“这是一个测试”

另一种使用re的简单方法是

import re, clr

text = 'some string... this part will be removed.'

text= re.search(r'(\A.*)\.\.\..+',url,re.DOTALL|re.IGNORECASE).group(1)

// text = some string

find方法将返回字符串中的字符位置。然后,如果你想从角色中删除所有东西,这样做:

mystring = "123⋯567"
mystring[ 0 : mystring.index("⋯")]

>> '123'

如果你想保留字符,在字符位置上加1。

假设你的分隔符是'…',但它可以是任何字符串。

text = 'some string... this part will be removed.'
head, sep, tail = text.partition('...')

>>> print head
some string

如果分隔符未找到,head将包含所有原始字符串。

分区函数是在Python 2.5中添加的。

S.partition(sep) ->(头,sep,尾) 搜索S中的分隔符sep,并返回它之前的部分, 分离器本身和后面的部分。如果分隔符不是 found返回S和两个空字符串。

还有一种方法是删除字符串中字符最后一次出现之后的所有字符(假设您想删除最后一个'/'之后的所有字符)。

path = 'I/only/want/the/containing/directory/not/the/file.txt'

while path[-1] != '/':
    path = path[:-1]