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 .
In Python
this could be:
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:]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.