简体   繁体   English

如何删除 Python 中特定字符之前的所有字符?

[英]How to remove all characters before a specific character in Python?

I'd like to remove all characters before a designated character or set of characters (for example):我想删除指定字符或字符集之前的所有字符(例如):

intro = "<>I'm Tom."

Now I'd like to remove the <> before I'm (or more specifically, I ).现在我想在I'm (或更具体地说, I )之前删除<> Any suggestions?有什么建议么?

Use re.sub .使用re.sub Just match all the chars upto I then replace the matched chars with I .只需匹配所有字符直到I然后用I替换匹配的字符。

re.sub(r'^.*?I', 'I', stri)

str.find could find character index of certain string's first appearance : str.find可以找到certain string's first appearance字符索引:

intro[intro.find('I'):]

Since index(char) gets you the first index of the character, you can simply do string[index(char):] .由于index(char)为您提供了字符的第一个索引,因此您可以简单地执行string[index(char):]

For example, in this case index("I") = 2 , and intro[2:] = "I'm Tom."例如,在本例中index("I") = 2intro[2:] = "I'm Tom."

If you know the character position of where to start deleting, you can use slice notation:如果知道从哪里开始删除的字符位置,可以使用切片表示法:

intro = intro[2:]

Instead of knowing where to start, if you know the characters to remove then you could use the lstrip() function:如果您知道要删除的字符,那么您可以使用lstrip()函数,而不是知道从哪里开始:

intro = intro.lstrip("<>")
str = "<>I'm Tom."
temp = str.split("I",1)
temp[0]=temp[0].replace("<>","")
str = "I".join(temp)

I looped through the string and passed the index.我遍历了字符串并传递了索引。

intro_list = []

intro = "<>I'm Tom."
for i in range(len(intro)):
    if intro[i] == '<' or intro[i] == '>':
        pass
    else:
        intro_list.append(intro[i])

intro = ''.join(intro_list)
print(intro)
>>> intro = "<>I'm Tom." #Just split the string at the special symbol >>> intro.split("<>") Output = ['', "I'm Tom."] >>> new = intro.split("<>") >>> new[1] "I'm Tom."
import re

date_div = "Blah blah\nblah, Updated: Aug. 23, 2012 Blah blah Updated: Feb. 13, 2019"

up_to_word = ":"
rx_to_first = r'^.*?{}'.format(re.escape(up_to_word))
rx_to_last = r'^.*{}'.format(re.escape(up_to_word))

# (Dot.) In the default mode, this matches any character except a newline. 
# If the DOTALL flag has been specified, this matches any character including a newline.

print("Remove all up to the first occurrence of the word including it:")
print(re.sub(rx_to_first, '', date_div, flags=re.DOTALL).strip())

print("Remove all up to the last occurrence of the word including it:")
print(re.sub(rx_to_last, '', date_div, flags=re.DOTALL).strip())

I'd like to remove all characters before a designated character or set of characters (for example):我想删除指定字符或字符集之前的所有字符(例如):

intro = "<>I'm Tom."

Now I'd like to remove the <> before I'm (or more specifically, I ).现在我想在I'm (或更具体地说, I )之前删除<> Any suggestions?有什么建议么?

This solution works if the character is not in the string too, but uses if statements which can be slow.如果字符也不在字符串中,则此解决方案有效,但使用可能很慢的 if 语句。

if 'I' in intro:
  print('I' + intro.split('I')[1])
else:
  print(intro)

You can use itertools.dropwhile to all the characters before seeing a character to stop at.在看到要停止的字符之前,您可以对所有字符使用itertools.dropwhile Then, you can use ''.join() to turn the resulting iterable back into a string:然后,您可以使用''.join()将生成的可迭代对象转回字符串:

from itertools import dropwhile
''.join(dropwhile(lambda x: x not in stop, intro))

This outputs:这输出:

I'm Tom.

Based on the @AvinashRaj answer, you can use re.sub to substituate a substring by a string or a character thanks to regex:基于@AvinashRaj 的回答,由于正则表达式,您可以使用 re.sub 将子字符串替换为字符串或字符:

missing import re

output_str = re.sub(r'^.*?I', 'I', input_str)

没有正则表达式

intro.split('<>',1)[1]
import re
intro = "<>I'm Tom."
re.sub(r'<>I', 'I', intro)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何删除python中特定字符后的所有字符? - How to remove all characters after a specific character in python? Pandas:删除特定字符重复4次的dataframe列中特定字符(最后一个特定字符)之前的所有字符 - Pandas: Remove all characters before a specific character (last specific character) in a dataframe column that specific character is repeated 4 times Pandas:删除 dataframe 列中特定字符之前的所有字符 - Pandas: Remove all characters before a specific character in a dataframe column 如何在Python中删除冒号之前的所有字符 - How to remove all characters before colon in python 如何在特定字符之前从字符串中删除特殊字符? - How to remove special characters from a string before specific character? 如何删除 python 中 pandas df 列中某个字符之前的所有字符 - How to remove all characters before a certain character in pandas df column in python Python:如何在固定字符串中的特定字符之前排除一组字符 - Python: How to exclude a group of characters before a specific character in a fixed string 在python中的separator(character)之前和之后删除字符 - remove characters before and after separator(character) in python 在Python中,如何删除特定字符周围的字符? - In Python, how can I remove characters around a specific character? 如何使用 Python 3 删除某个字符后的所有字符 - How to remove all characters after a certain character using Python 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM