如何删除python中特定字符后的所有字符?

时间:2022-01-30 20:24:37

I have a string. How do I remove all text after a certain character? (In this case ...)
The text after will ... change so I that's why I want to remove all characters after a certain one.

我有一个字符串。如何删除某个字符后的所有文字? (在这种情况下......)之后的文本将...改变所以我这就是为什么我要删除所有字符后的某个字符。

5 个解决方案

#1


136  

Split on your separator at most once, and take the first piece:

最多在分隔符上拆分一次,然后取出第一块:

sep = '...'
rest = text.split(sep, 1)[0]

You didn't say what should happen if the separator isn't present. Both this and Alex's solution will return the entire string in that case.

你没有说如果没有分隔符会发生什么。在这种情况下,这个和Alex的解决方案都将返回整个字符串。

#2


59  

Assuming your separator is '...', but it can be any string.

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

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

>>> print head
some string

If the separator is not found, head will contain all of the original string.

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

The partition function was added in Python 2.5.

分区函数在Python 2.5中添加。

partition(...) S.partition(sep) -> (head, sep, tail)

分区(...)S.partition(sep) - >(head,sep,tail)

Searches for the separator sep in S, and returns the part before it,
the separator itself, and the part after it.  If the separator is not
found, returns S and two empty strings.

#3


7  

Without a RE (which I assume is what you want):

没有RE(我认为是你想要的):

def remafterellipsis(text):
  where_ellipsis = text.find('...')
  if where_ellipsis == -1:
    return text
  return text[:where_ellipsis + 3]

or, with a RE:

或者,有RE:

import re

def remwithre(text, there=re.compile(re.escape('...')+'.*')):
  return there.sub('', text)

#4


7  

If you want to remove everything after the last occurrence of separator in a string I find this works well:

如果要在字符串中最后一次出现分隔符后删除所有内容,我发现这很有效:

<separator>.join(string_to_split.split(<separator>)[:-1])

<隔板> 。加入(string_to_split.split( <隔板> ): - 1])

For example, if string_to_split is a path like root/location/child/too_far.exe and you only want the folder path, you can split by "/".join(string_to_split.split("/")[:-1]) and you'll get root/location/child

例如,如果string_to_split是root / location / child / too_far.exe之类的路径,并且您只想要文件夹路径,则可以通过“/".join(string_to_split.split("/")[:-1]拆分)你会得到root / location / child

#5


0  

another easy way using re will be

使用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

#1


136  

Split on your separator at most once, and take the first piece:

最多在分隔符上拆分一次,然后取出第一块:

sep = '...'
rest = text.split(sep, 1)[0]

You didn't say what should happen if the separator isn't present. Both this and Alex's solution will return the entire string in that case.

你没有说如果没有分隔符会发生什么。在这种情况下,这个和Alex的解决方案都将返回整个字符串。

#2


59  

Assuming your separator is '...', but it can be any string.

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

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

>>> print head
some string

If the separator is not found, head will contain all of the original string.

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

The partition function was added in Python 2.5.

分区函数在Python 2.5中添加。

partition(...) S.partition(sep) -> (head, sep, tail)

分区(...)S.partition(sep) - >(head,sep,tail)

Searches for the separator sep in S, and returns the part before it,
the separator itself, and the part after it.  If the separator is not
found, returns S and two empty strings.

#3


7  

Without a RE (which I assume is what you want):

没有RE(我认为是你想要的):

def remafterellipsis(text):
  where_ellipsis = text.find('...')
  if where_ellipsis == -1:
    return text
  return text[:where_ellipsis + 3]

or, with a RE:

或者,有RE:

import re

def remwithre(text, there=re.compile(re.escape('...')+'.*')):
  return there.sub('', text)

#4


7  

If you want to remove everything after the last occurrence of separator in a string I find this works well:

如果要在字符串中最后一次出现分隔符后删除所有内容,我发现这很有效:

<separator>.join(string_to_split.split(<separator>)[:-1])

<隔板> 。加入(string_to_split.split( <隔板> ): - 1])

For example, if string_to_split is a path like root/location/child/too_far.exe and you only want the folder path, you can split by "/".join(string_to_split.split("/")[:-1]) and you'll get root/location/child

例如,如果string_to_split是root / location / child / too_far.exe之类的路径,并且您只想要文件夹路径,则可以通过“/".join(string_to_split.split("/")[:-1]拆分)你会得到root / location / child

#5


0  

another easy way using re will be

使用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