简体   繁体   English

如何根据条件从字符串的开头删除文本?

[英]How to remove text from the beginning of a string based on condition?

suppose i have the following text:假设我有以下文字:

'Reuters - Life is beautiful.'
'agency.com - China\'s currency remains pegged to the dollar and the US currency\'s sharp falls in recent months have therefore made - Chinese export prices highly competitive.'
'AP - The number of days that beaches closed or posted warnings because of pollution rose sharply in 2003 due to more rainfall, increased monitoring and tougher -standards, an environmental group said on Thursday.'
'CNN - Warming water temperatures - in the central equatorial Pacific last month may indicate the start of a new El Nino.'

I want to remove all text only at the beginning of the text that's before a "-" and only if the "-" is followed by a whitespace.我只想删除位于"-"之前的文本开头的所有文本,并且仅当"-"后跟一个空格时。

I would like something like:我想要类似的东西:

if line.startswith(code_to_match_my_condition):
      strip_matched_text_from_line

So the results would be:所以结果将是:

'Life is beautiful.'
'China\'s currency remains pegged to the dollar and the US currency\'s sharp falls in recent months have therefore made - Chinese export prices highly competitive.'
'The number of days that beaches closed or posted warnings because of pollution rose sharply in 2003 due to more rainfall, increased monitoring and tougher -standards, an environmental group said on Thursday.'
'Warming water temperatures - in the central equatorial Pacific last month may indicate the start of a new El Nino.'

I actually don't know how to code this.我实际上不知道如何编码。 I would appreciate any help on this.我将不胜感激。

Thank you very much in advance非常感谢您提前

You might want to use a regular expression, such as您可能想要使用正则表达式,例如

^[^-]+-\s+

and replace this with an empty string, see a demo on regex101.com .并将其替换为空字符串,请参阅regex101.com 上的演示


In Python this could be:Python这可能是:

import re

strings = ['Reuters - Life is beautiful.',
           'agency.com - China\'s currency remains pegged to the dollar and the US currency\'s sharp falls in recent months have therefore made - Chinese export prices highly competitive.',
           'AP - The number of days that beaches closed or posted warnings because of pollution rose sharply in 2003 due to more rainfall, increased monitoring and tougher -standards, an environmental group said on Thursday.',
           'CNN - Warming water temperatures - in the central equatorial Pacific last month may indicate the start of a new El Nino.']

rx = re.compile(r'^[^-]+-\s+')

strings = list(map(lambda string: rx.sub("", string), strings))
print(strings)

And yields和产量

['Life is beautiful.', "China's currency remains pegged to the dollar and the US currency's sharp falls in recent months have therefore made - Chinese export prices highly competitive.", 'The number of days that beaches closed or posted warnings because of pollution rose sharply in 2003 due to more rainfall, increased monitoring and tougher -standards, an environmental group said on Thursday.', 'Warming water temperatures - in the central equatorial Pacific last month may indicate the start of a new El Nino.']

Simple, readable code to search and remove the block:用于搜索和删除块的简单、可读的代码:

if " - " in line:
    index = line.find(" - ")
    line = line[index+3:]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM