最多一次放在分离器上,然后拿第一件:
...
sep = '...'
stripped = text.split(sep, 1)[0]
root/location/child
或带有正则表达式:
def remafterellipsis(text):
where_ellipsis = text.find('...')
if where_ellipsis == -1:
return text
return text[:where_ellipsis + 3]
import re
def remwithre(text, there=re.compile(re.escape('...')+'.*')):
return there.sub('', text)
输出:“这是一个测试”方法查找将返回字符串中的字符位置。然后,如果您想从角色中删除所有内容,请执行此操作:
import re
test = "This is a test...we should not be able to see this"
res = re.sub(r'\.\.\..*',"",test)
print(res)
如果您想保持角色,请在字符位置上加1个。
从文件:
mystring = "123⋯567"
mystring[ 0 : mystring.index("⋯")]
>> '123'
替换替代者:
import re
sep = '...'
with open("requirements.txt") as file_in:
lines = []
for line in file_in:
res = line.split(sep, 1)[0]
print(res)
这在Python 3.7为我工作 在我的情况下,我需要在DOT中删除字符串可变费用
substring = split_string [0]PRINT(substring)
在字符串中出现字符之后,删除所有字符的另一种方法(假设您想在最终'/':)之后删除所有字符。
text, *_ = text.partition('...')
使用path = 'I/only/want/the/containing/directory/not/the/file.txt'
while path[-1] != '/':
path = path[:-1]
如果您需要区分第一和最后一次,则可能会派上用场。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
str.find