我試圖從文本中提取一些特定的日期。文本如下所示:
'Shares of Luxury Goods Makers Slip on Russia Export Ban',
'By Investing.com\xa0-\xa0Mar 15, 2022 By Dhirendra Tripathi',
'Investing.com – Stocks of European retailers such as LVMH (PA:LVMH), Kering (PA:PRTP), H&M (ST:HMb), Moncler (MI:MONC) and Hermès (PA:HRMS) were all down around 4% Tuesday... ',
'',
'',
'',
' ',
'Europe Stocks Open Lower as Wider Sanctions, Covid Rebound Hit Mood',
'By Investing.com\xa0-\xa0Mar 15, 2022 By Geoffrey Smith\xa0',
'Investing.com -- European stock markets opened lower on Tuesday as a fresh round of EU sanctions, a rebound in Covid-19 cases and more signs of red-hot inflation all weighed on... ',
'',
'\xa0',
顯然,在這個小片段中,id 只想提取:2022 年 3 月 15 日和 2022 年 3 月 15 日。
我嘗試過:
datefinder.find_dates(text)
dateutil.parser
第一個回傳我想要的所有日期以及大量不存在的其他日期。
第二個回傳“字串不包含日期:”
誰能想到我能做到這一點的最好方法?
uj5u.com熱心網友回復:
你可以使用正則運算式
import re
line = r'By Investing.com\xa0-\xa0Mar 15, 2022 By Geoffrey Smith\xa0'
re_results = re.findall(r'[A-Z][a-z]{2} \d{1,2}, \d{4}', line)
for result in re_results:
print(result)
輸出:
Mar 15, 2022
您可以在這里測驗正則運算式https://regexr.com/
uj5u.com熱心網友回復:
使用re:
import re
x = ['Shares of Luxury Goods Makers Slip on Russia Export Ban',
'By Investing.com\xa0-\xa0Mar 15, 2022 By Dhirendra Tripathi',
'Investing.com – Stocks of European retailers such as LVMH (PA:LVMH), Kering (PA:PRTP), H&M (ST:HMb), Moncler (MI:MONC) and Hermès (PA:HRMS) were all down around 4% Tuesday... ',
'',
'',
'',
' ',
'Europe Stocks Open Lower as Wider Sanctions, Covid Rebound Hit Mood',
'By Investing.com\xa0-\xa0Mar 15, 2022 By Geoffrey Smith\xa0',
'Investing.com -- European stock markets opened lower on Tuesday as a fresh round of EU sanctions, a rebound in Covid-19 cases and more signs of red-hot inflation all weighed on... ',
'',
'\xa0', ]
for line in x:
m = re.search(r'\w{3} \d{1,2}, \d{4}', line)
if m:
print(m.group())
輸出:
Mar 15, 2022
Mar 15, 2022
請注意,這只會以以下形式匹配日期[3 letters] [1-2 numbers] [4 numbers]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/475141.html
上一篇:在oracle中插入時間值
