Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何删除出现在';更多:';在给定的字符串中?_Python 3.x - Fatal编程技术网

Python 3.x 如何删除出现在';更多:';在给定的字符串中?

Python 3.x 如何删除出现在';更多:';在给定的字符串中?,python-3.x,Python 3.x,对于我搜集的新闻文章,我试图省略“MORE:”之后的句子,因为这是一个反向链接,与文章无关。 首相宣布了一个新的新的冠状病毒警报系统来跟踪冠状病毒R的数量——病毒的繁殖速度——这将决定国家如何逐步解除封锁。他说,英国的R数字现在在0.5到0.9之间。如果数据支持的话,最早在6月,一些学校可以开学,到7月,等待进一步的科学建议,政府希望酒店业的领域能够重新开放 更多:牛津试验中的情况如何引领冠状病毒疫苗的竞争 如果我们作为一个国家开始满足我提出的条件,那么在接下来的几个星期和几个月里,我们也许能够

对于我搜集的新闻文章,我试图省略“MORE:”之后的句子,因为这是一个反向链接,与文章无关。

首相宣布了一个新的新的冠状病毒警报系统来跟踪冠状病毒R的数量——病毒的繁殖速度——这将决定国家如何逐步解除封锁。他说,英国的R数字现在在0.5到0.9之间。如果数据支持的话,最早在6月,一些学校可以开学,到7月,等待进一步的科学建议,政府希望酒店业的领域能够重新开放

更多:牛津试验中的情况如何引领冠状病毒疫苗的竞争

如果我们作为一个国家开始满足我提出的条件,那么在接下来的几个星期和几个月里,我们也许能够走得更远

因此,对于上面的示例,我希望python程序删除“MORE:”后面的一行,直到检测到一个新行,即“\n”,这意味着文章将从此处继续


PS-我知道在这种情况下最有可能使用正则表达式,但我不确定如何使用。

您可以使用regex方法实现这一点

所以你的模式是

pattern = r'(?=MORE:)(.*)'
模式说明:

  • (?=MORE:)
    :将匹配表达式
    MORE:
    后的任何组,而不将其包含在结果中
  • (.*)
    :将0或任何非换行符的字符分组
python中的最终代码将是

import re
pattern = r'(?=MORE:)(.*)'
target_string ='''
The prime minister announced a new COVID Alert System to track the coronavirus R number -- the rate at which the virus reproduces -- that will determine how the country phases out of lockdown. The R number is now between 0.5 and 0.9 in the U.K., he said. If the data supports it, by June at the earliest some schools could open, and by July, pending “further scientific advice,†the government hope that areas of the hospitality industry may be able to reopen.

MORE: What it's like inside the Oxford trial leading the race for coronavirus vaccine

If we as a nation begin to fulfil the conditions I have set out, then in the next few weeks and months we may be able to go further.....'''
without_more = re.sub(r'(?=MORE:)(.*)','',target_string)
print(without_more)

另一种解决方案:

without_more=target_string[:target_string.index('MORE')]
print(str(without_more))

你能提供一些你已经尝试过的代码来获取文章内容吗?谢谢