{"id":1139971,"date":"2025-01-08T22:15:32","date_gmt":"2025-01-08T14:15:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1139971.html"},"modified":"2025-01-08T22:15:34","modified_gmt":"2025-01-08T14:15:34","slug":"python%e5%a6%82%e4%bd%95%e6%8f%90%e5%8f%96%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b8%ad%e7%9a%84%e6%97%a5%e6%9c%9f%e6%a0%bc%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1139971.html","title":{"rendered":"python\u5982\u4f55\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u65e5\u671f\u683c\u5f0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103121\/cf009deb-22bc-4b5d-af96-5d5c6cacfbbd.webp\" alt=\"python\u5982\u4f55\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u65e5\u671f\u683c\u5f0f\" \/><\/p>\n<p><p> <strong>Python\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u65e5\u671f\u683c\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u65e5\u671f\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u65e5\u671f\u89e3\u6790\u5e93\u548c\u5b57\u7b26\u4e32\u64cd\u4f5c\u65b9\u6cd5\u7b49\u65b9\u5f0f\u3002<strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u65e5\u671f\u89e3\u6790\u5e93\uff08\u5982dateutil\uff09\u3001\u5b57\u7b26\u4e32\u64cd\u4f5c<\/strong>\u662f\u6700\u5e38\u7528\u7684\u4e09\u79cd\u65b9\u6cd5\u3002\u5176\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u6700\u4e3a\u7075\u6d3b\u548c\u5f3a\u5927\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u65e5\u671f\u683c\u5f0f\u81ea\u5b9a\u4e49\u5339\u914d\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u65e5\u671f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\uff08Regular Expression\uff09\u662f\u5904\u7406\u5b57\u7b26\u4e32\u7684\u5f3a\u5927\u5de5\u5177\u3002\u5b83\u5141\u8bb8\u6211\u4eec\u5b9a\u4e49\u590d\u6742\u7684\u6a21\u5f0f\u6765\u5339\u914d\u4e0d\u540c\u5f62\u5f0f\u7684\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_dates(text):<\/p>\n<p>    # \u5339\u914d\u683c\u5f0f\uff1aYYYY-MM-DD<\/p>\n<p>    date_pattern = r&#39;\\b\\d{4}-\\d{2}-\\d{2}\\b&#39;<\/p>\n<p>    dates = re.findall(date_pattern, text)<\/p>\n<p>    return dates<\/p>\n<p>text = &quot;The events are scheduled on 2023-09-23 and 2023-12-01.&quot;<\/p>\n<p>print(extract_dates(text))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u6765\u5339\u914d\u65e5\u671f\u683c\u5f0f\u4e3aYYYY-MM-DD\u3002<code>re.findall<\/code>\u65b9\u6cd5\u8fd4\u56de\u5339\u914d\u5230\u7684\u6240\u6709\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528dateutil\u5e93\u89e3\u6790\u65e5\u671f<\/h3>\n<\/p>\n<p><p>Python\u7684dateutil\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u65e5\u671f\u89e3\u6790\u529f\u80fd\uff0c\u53ef\u4ee5\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u5e76\u89e3\u6790\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from dateutil.parser import parse<\/p>\n<p>def extract_dates(text):<\/p>\n<p>    words = text.split()<\/p>\n<p>    dates = []<\/p>\n<p>    for word in words:<\/p>\n<p>        try:<\/p>\n<p>            dates.append(parse(word, fuzzy=False))<\/p>\n<p>        except ValueError:<\/p>\n<p>            continue<\/p>\n<p>    return dates<\/p>\n<p>text = &quot;The events are scheduled on 23rd September 2023 and 1st December 2023.&quot;<\/p>\n<p>print(extract_dates(text))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>dateutil.parser.parse<\/code>\u51fd\u6570\u5c1d\u8bd5\u89e3\u6790\u6bcf\u4e2a\u5355\u8bcd\uff0c\u5982\u679c\u89e3\u6790\u6210\u529f\u5219\u5c06\u5176\u52a0\u5165\u65e5\u671f\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5b57\u7b26\u4e32\u64cd\u4f5c\u63d0\u53d6\u65e5\u671f<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u4e5f\u53ef\u4ee5\u6709\u6548\u5730\u63d0\u53d6\u65e5\u671f\uff0c\u5c24\u5176\u662f\u5728\u65e5\u671f\u683c\u5f0f\u6bd4\u8f83\u56fa\u5b9a\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def extract_dates(text):<\/p>\n<p>    dates = []<\/p>\n<p>    words = text.split()<\/p>\n<p>    for word in words:<\/p>\n<p>        if &#39;-&#39; in word and len(word) == 10:<\/p>\n<p>            try:<\/p>\n<p>                year, month, day = map(int, word.split(&#39;-&#39;))<\/p>\n<p>                if 1 &lt;= month &lt;= 12 and 1 &lt;= day &lt;= 31:<\/p>\n<p>                    dates.append(word)<\/p>\n<p>            except ValueError:<\/p>\n<p>                continue<\/p>\n<p>    return dates<\/p>\n<p>text = &quot;The events are scheduled on 2023-09-23 and 2023-12-01.&quot;<\/p>\n<p>print(extract_dates(text))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u5206\u5272\u548c\u68c0\u67e5\u6765\u63d0\u53d6\u65e5\u671f\u683c\u5f0f\u4e3aYYYY-MM-DD\u7684\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7efc\u5408\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u7efc\u5408\u4f7f\u7528\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u4ee5\u5904\u7406\u4e0d\u540c\u7684\u65e5\u671f\u683c\u5f0f\u548c\u6587\u672c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>from dateutil.parser import parse<\/p>\n<p>def extract_dates(text):<\/p>\n<p>    # \u5b9a\u4e49\u591a\u79cd\u65e5\u671f\u683c\u5f0f\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<\/p>\n<p>    patterns = [<\/p>\n<p>        r&#39;\\b\\d{4}-\\d{2}-\\d{2}\\b&#39;,  # YYYY-MM-DD<\/p>\n<p>        r&#39;\\b\\d{2}\/\\d{2}\/\\d{4}\\b&#39;,  # MM\/DD\/YYYY<\/p>\n<p>        r&#39;\\b\\d{2}-\\d{2}-\\d{4}\\b&#39;,  # DD-MM-YYYY<\/p>\n<p>    ]<\/p>\n<p>    dates = []<\/p>\n<p>    for pattern in patterns:<\/p>\n<p>        matches = re.findall(pattern, text)<\/p>\n<p>        for match in matches:<\/p>\n<p>            try:<\/p>\n<p>                # \u5c1d\u8bd5\u89e3\u6790\u5339\u914d\u5230\u7684\u65e5\u671f<\/p>\n<p>                dates.append(parse(match, fuzzy=False))<\/p>\n<p>            except ValueError:<\/p>\n<p>                continue<\/p>\n<p>    # \u4f7f\u7528dateutil\u89e3\u6790\u5269\u4f59\u7684\u6587\u672c<\/p>\n<p>    words = text.split()<\/p>\n<p>    for word in words:<\/p>\n<p>        try:<\/p>\n<p>            dates.append(parse(word, fuzzy=False))<\/p>\n<p>        except ValueError:<\/p>\n<p>            continue<\/p>\n<p>    return dates<\/p>\n<p>text = &quot;The events are scheduled on 2023-09-23, 23\/09\/2023, and December 1, 2023.&quot;<\/p>\n<p>print(extract_dates(text))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u7ed3\u5408\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\u548cdateutil\u5e93\u6765\u5904\u7406\u591a\u79cd\u683c\u5f0f\u7684\u65e5\u671f\u3002\u8fd9\u79cd\u65b9\u6cd5\u66f4\u52a0\u901a\u7528\u548c\u7075\u6d3b\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u6587\u672c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u6548\u5730\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u65e5\u671f\u683c\u5f0f\uff0c\u5e76\u6839\u636e\u9700\u8981\u8fdb\u884c\u89e3\u6790\u548c\u5904\u7406\u3002\u65e0\u8bba\u662f\u5904\u7406\u65e5\u5fd7\u6587\u4ef6\u3001\u6570\u636e\u5206\u6790\u8fd8\u662f\u6587\u672c\u6316\u6398\uff0c\u8fd9\u4e9b\u6280\u5de7\u90fd\u975e\u5e38\u5b9e\u7528\u548c\u91cd\u8981\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bc6\u522b\u4e0d\u540c\u683c\u5f0f\u7684\u65e5\u671f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff08<code>re<\/code>\u6a21\u5757\uff09\u6765\u8bc6\u522b\u591a\u79cd\u65e5\u671f\u683c\u5f0f\u3002\u901a\u8fc7\u5b9a\u4e49\u9002\u5f53\u7684\u6a21\u5f0f\uff0c\u53ef\u4ee5\u5339\u914d\u5982\u201cYYYY-MM-DD\u201d\uff0c\u201cDD\/MM\/YYYY\u201d\uff0c\u201cMM-DD-YYYY\u201d\u7b49\u4e0d\u540c\u683c\u5f0f\u7684\u65e5\u671f\u3002\u4f7f\u7528<code>re.findall()<\/code>\u51fd\u6570\u53ef\u4ee5\u63d0\u53d6\u51fa\u7b26\u5408\u8fd9\u4e9b\u683c\u5f0f\u7684\u6240\u6709\u65e5\u671f\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u63d0\u53d6\u65e5\u671f\u65f6\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u5e93\uff1f<\/strong><br \/>\u5728\u5904\u7406\u65e5\u671f\u63d0\u53d6\u65f6\uff0c<code>dateutil<\/code>\u5e93\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u89e3\u6790\u591a\u79cd\u65e5\u671f\u683c\u5f0f\uff0c\u8fd8\u80fd\u5904\u7406\u4e00\u4e9b\u6a21\u7cca\u7684\u65e5\u671f\u5b57\u7b26\u4e32\u3002<code>pandas<\/code>\u5e93\u540c\u6837\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u65e5\u671f\u5904\u7406\u529f\u80fd\uff0c\u5c24\u5176\u9002\u5408\u5904\u7406\u5e26\u6709\u65f6\u95f4\u5e8f\u5217\u7684\u6570\u636e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u63d0\u53d6\u5230\u7684\u65e5\u671f\u6570\u636e\uff1f<\/strong><br \/>\u63d0\u53d6\u5230\u7684\u65e5\u671f\u6570\u636e\u901a\u5e38\u662f\u5b57\u7b26\u4e32\u683c\u5f0f\u3002\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u5c06\u5176\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\uff0c\u4ee5\u4fbf\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u64cd\u4f5c\uff0c\u5982\u6bd4\u8f83\u3001\u683c\u5f0f\u5316\u6216\u8ba1\u7b97\u65f6\u95f4\u5dee\u7b49\u3002\u4f7f\u7528<code>datetime.strptime()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\uff0c\u65b9\u4fbf\u540e\u7eed\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u65e5\u671f\u683c\u5f0f \u5728Python\u4e2d\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u65e5\u671f\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u65e5\u671f\u89e3\u6790\u5e93\u548c\u5b57 [&hellip;]","protected":false},"author":3,"featured_media":1139981,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1139971"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1139971"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1139971\/revisions"}],"predecessor-version":[{"id":1139983,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1139971\/revisions\/1139983"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1139981"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1139971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1139971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1139971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}