问题:如何在python中删除特定字符之后的所有字符?

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

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.


回答 0

最多在分隔器上分割一次,然后取下第一块:

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

您没有说如果不使用分隔符该怎么办。在这种情况下,此方法和Alex的解决方案都将返回整个字符串。

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

sep = '...'
stripped = 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.


回答 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)->(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.

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.

The partition function was added in Python 2.5.

partition(…) 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.

回答 2

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

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

例如,如果 string_to_split是像一个路径root/location/child/too_far.exe,你只需要在文件夹路径,您可以通过拆分"/".join(string_to_split.split("/")[:-1]),你会得到 root/location/child

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])

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


回答 3

没有RE(我想是您想要的):

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

或者,使用RE:

import re

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

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

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

or, with a RE:

import re

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

回答 4

find方法将返回字符串中的字符位置。然后,如果要从角色中删除所有内容,请执行以下操作:

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

>> '123'

如果要保留字符,请在字符位置加1。

The method find will return the character position in a string. Then, if you want remove every thing from the character, do this:

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

>> '123'

If you want to keep the character, add 1 to the character position.


回答 5

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

输出:“这是一个测试”

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

Output: “This is a test”


回答 6

从文件中:

import re
sep = '...'

with open("requirements.txt") as file_in:
    lines = []
    for line in file_in:
        res = line.split(sep, 1)[0]
        print(res)

From a file:

import re
sep = '...'

with open("requirements.txt") as file_in:
    lines = []
    for line in file_in:
        res = line.split(sep, 1)[0]
        print(res)

回答 7

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

another easy way using re will be

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

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。