📜  Python - 在字符串中提取日期

📅  最后修改于: 2022-05-13 01:55:41.284000             🧑  作者: Mango

Python - 在字符串中提取日期

给定一个字符串,任务是编写一个Python程序来从中提取日期。

Input : test_str = "gfg at 2021-01-04"
Output : 2021-01-04
Explanation : Date format string found.

Input : test_str = "2021-01-04 for gfg"
Output : 2021-01-04
Explanation : Date format string found.

方法 #1:使用re.search() + strptime() 方法

在这种情况下,特定日期的搜索组被输入 search(),而 strptime() 用于输入要搜索的格式。

Python3
# Python3 code to demonstrate working of
# Detect date in String
# Using re.search() + strptime()
import re
from datetime import datetime
  
# initializing string
test_str = "gfg at 2021-01-04"
  
# printing original string
print("The original string is : " + str(test_str))
  
# searching string
match_str = re.search(r'\d{4}-\d{2}-\d{2}', test_str)
  
# computed date
# feeding format
res = datetime.strptime(match_str.group(), '%Y-%m-%d').date()
  
# printing result
print("Computed date : " + str(res))


Python3
# Python3 code to demonstrate working of
# Detect date in String
# Using python-dateutil()
from dateutil import parser
  
# initializing string
test_str = "gfg at 2021-01-04"
  
# printing original string
print("The original string is : " + str(test_str))
  
# extracting date using inbuilt func.
res = parser.parse(test_str, fuzzy=True)
  
# printing result
print("Computed date : " + str(res)[:10])


输出:

The original string is : gfg at 2021-01-04
Computed date : 2021-01-04

方法 #2:使用python-dateutil()模块



这是解决这个问题的另一种方法。在这个内置的Python库 python-dateutil 中, parse() 方法可用于检测字符串的日期和时间。

蟒蛇3

# Python3 code to demonstrate working of
# Detect date in String
# Using python-dateutil()
from dateutil import parser
  
# initializing string
test_str = "gfg at 2021-01-04"
  
# printing original string
print("The original string is : " + str(test_str))
  
# extracting date using inbuilt func.
res = parser.parse(test_str, fuzzy=True)
  
# printing result
print("Computed date : " + str(res)[:10])

输出:

The original string is : gfg at 2021-01-04
Computed date : 2021-01-04