我有如下所示的大句子,
how are you
On Tue, Dec 21, 2021 at 1:51 PM <abc<http://localhost>> wrote:
-------------------------------------------------------------
NOTE: Please do not remove email address from the \"To\" line of this email when replying. This address is used to capture the email and report it. Please do not remove or change the subject line of this email. The subject line of this email contains information to refer this correspondence back to the originating discrepancy.
我想要句子 ( Tue, Dec 21, 2021 at 1:51 PM) 中指定的日期和時間。如何從句子中提取?
uj5u.com熱心網友回復:
去這里的方法是使用正則運算式,但為了簡單起見,如果文本的格式始終相同,您可以通過查找如下所示的行來獲取日期字串On SOME DATE <Someone<someone's email address>> wrote:。這是一個示例實作:
email = """how are you
On Tue, Dec 21, 2021 at 1:51 PM <abc<http://localhost>> wrote:
-------------------------------------------------------------
NOTE: Please do not remove email address from the \"To\" line of this email when replying. This address is used to capture the email and report it. Please do not remove or change the subject line of this email. The subject line of this email contains information to refer this correspondence back to the originating discrepancy."""
for line in email.splitlines():
if line.startswith("On ") and line.endswith(" wrote:"):
date_string = line[3 : line.index(" <")]
print(f"Found the date: {date_string!r}")
break
else:
print("Could not find the date.")
uj5u.com熱心網友回復:
很骯臟:
string = """how are you \r\n\r\nOn Tue, Dec 21, 2021 at 1:51 PM
<abchttp://localhost> wrote:\r\n\r\n\r\n---------------------------------
----------------------------\r\nNOTE: Please do not remove email address
from the"To" line of this email when replying.This address is used to
capture the email and report it.Please do not remove or change the
subject line of this email.The subject line of this email contains
information to refer this correspondence back to the originating
discrepancy.\r\n"""
string = string.split("\r\n\r\n")
date = ' '.join(string[1].split(' ')[:8])
print(date)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/392885.html
上一篇:當用戶嘗試分享帖子時,函式視圖在/group/share/42/處拋出UnboundLocalError例外
下一篇:戰斗和詞形還原的基礎詞
