{"id":1082551,"date":"2025-01-08T12:49:58","date_gmt":"2025-01-08T04:49:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1082551.html"},"modified":"2025-01-08T12:50:01","modified_gmt":"2025-01-08T04:50:01","slug":"%e6%97%b6%e5%88%86%e7%a7%92%e6%95%b0%e6%8d%aepython%e5%a6%82%e4%bd%95%e6%8f%90%e5%8f%96-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1082551.html","title":{"rendered":"\u65f6\u5206\u79d2\u6570\u636ePython\u5982\u4f55\u63d0\u53d6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24184016\/60db93b6-1ecf-454b-84d9-c9c7cad0c193.webp\" alt=\"\u65f6\u5206\u79d2\u6570\u636ePython\u5982\u4f55\u63d0\u53d6\" \/><\/p>\n<p><p> <strong>\u65f6\u5206\u79d2\u6570\u636e\u63d0\u53d6\u7684\u65b9\u5f0f\u5305\u62ec\uff1a\u4f7f\u7528datetime\u6a21\u5757\u3001\u4f7f\u7528time\u6a21\u5757\u3001\u4f7f\u7528pandas\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u63d0\u53d6\u65f6\u5206\u79d2\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528datetime\u6a21\u5757\u3001time\u6a21\u5757\u3001\u4ee5\u53capandas\u6a21\u5757\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\u7684\u4e00\u79cd\u3002<\/p>\n<\/p>\n<p><p>datetime\u6a21\u5757\u63d0\u4f9b\u4e86\u64cd\u4f5c\u65e5\u671f\u548c\u65f6\u95f4\u7684\u7c7b\uff0c\u80fd\u8f7b\u677e\u5730\u63d0\u53d6\u65f6\u5206\u79d2\u6570\u636e\u3002\u901a\u8fc7\u4f7f\u7528datetime\u6a21\u5757\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2adatetime\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528\u5176\u5185\u7f6e\u7684\u65b9\u6cd5\u63d0\u53d6\u65f6\u95f4\u7684\u5404\u4e2a\u90e8\u5206\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528datetime\u6a21\u5757\u63d0\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u65f6\u5206\u79d2\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>now = datetime.now()<\/p>\n<h2><strong>\u63d0\u53d6\u65f6\u3001\u5206\u3001\u79d2<\/strong><\/h2>\n<p>hour = now.hour<\/p>\n<p>minute = now.minute<\/p>\n<p>second = now.second<\/p>\n<p>print(f&quot;\u5f53\u524d\u65f6\u95f4\u662f\uff1a{hour}\u65f6 {minute}\u5206 {second}\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e00\u3001\u4f7f\u7528DATETIME\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>datetime\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u64cd\u4f5c\u65e5\u671f\u548c\u65f6\u95f4\u7684\u7c7b\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528datetime\u6a21\u5757\u6765\u521b\u5efa\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61\uff0c\u5e76\u4ece\u4e2d\u63d0\u53d6\u65f6\u5206\u79d2\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u521b\u5efadatetime\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528datetime\u7c7b\u6765\u521b\u5efa\u4e00\u4e2a\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u521b\u5efa\u5f53\u524d\u65f6\u95f4\u7684datetime\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>now = datetime.now()<\/p>\n<p>print(now)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528datetime.now()\u65b9\u6cd5\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u53d8\u91cfnow\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u63d0\u53d6\u65f6\u5206\u79d2\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u4e00\u65e6\u4f60\u6709\u4e86\u4e00\u4e2adatetime\u5bf9\u8c61\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u5176\u5185\u7f6e\u7684\u65b9\u6cd5\u6765\u63d0\u53d6\u65f6\u5206\u79d2\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u63d0\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u65f6\u5206\u79d2\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u63d0\u53d6\u65f6\u3001\u5206\u3001\u79d2<\/p>\n<p>hour = now.hour<\/p>\n<p>minute = now.minute<\/p>\n<p>second = now.second<\/p>\n<p>print(f&quot;\u5f53\u524d\u65f6\u95f4\u662f\uff1a{hour}\u65f6 {minute}\u5206 {second}\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528hour\u3001minute\u548csecond\u5c5e\u6027\u6765\u63d0\u53d6\u65f6\u95f4\u7684\u5404\u4e2a\u90e8\u5206\uff0c\u5e76\u5c06\u5176\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u89e3\u6790\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u4ece\u5b57\u7b26\u4e32\u4e2d\u89e3\u6790\u65e5\u671f\u65f6\u95f4\u4fe1\u606f\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528datetime.strptime()\u65b9\u6cd5\u6765\u89e3\u6790\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32\uff0c\u5e76\u521b\u5efa\u4e00\u4e2adatetime\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u89e3\u6790\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32\u5e76\u63d0\u53d6\u65f6\u5206\u79d2\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u89e3\u6790\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32<\/p>\n<p>datetime_str = &quot;2023-10-05 14:30:45&quot;<\/p>\n<p>datetime_obj = datetime.strptime(datetime_str, &#39;%Y-%m-%d %H:%M:%S&#39;)<\/p>\n<h2><strong>\u63d0\u53d6\u65f6\u3001\u5206\u3001\u79d2<\/strong><\/h2>\n<p>hour = datetime_obj.hour<\/p>\n<p>minute = datetime_obj.minute<\/p>\n<p>second = datetime_obj.second<\/p>\n<p>print(f&quot;\u89e3\u6790\u540e\u7684\u65f6\u95f4\u662f\uff1a{hour}\u65f6 {minute}\u5206 {second}\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528datetime.strptime()\u65b9\u6cd5\u89e3\u6790\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32\uff0c\u5e76\u4f7f\u7528hour\u3001minute\u548csecond\u5c5e\u6027\u63d0\u53d6\u65f6\u95f4\u7684\u5404\u4e2a\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u4f7f\u7528TIME\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>time\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u4e0e\u65f6\u95f4\u76f8\u5173\u7684\u51fd\u6570\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528time\u6a21\u5757\u6765\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u5e76\u4ece\u4e2d\u63d0\u53d6\u65f6\u5206\u79d2\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528time\u6a21\u5757\u7684time()\u51fd\u6570\u6765\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\uff08\u4ee5\u79d2\u4e3a\u5355\u4f4d\u7684\u6d6e\u70b9\u6570\uff09\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\uff08\u4ee5\u79d2\u4e3a\u5355\u4f4d\u7684\u6d6e\u70b9\u6570\uff09<\/strong><\/h2>\n<p>current_time = time.time()<\/p>\n<p>print(current_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528time.time()\u51fd\u6570\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u53d8\u91cfcurrent_time\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u63d0\u53d6\u65f6\u5206\u79d2\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528time\u6a21\u5757\u7684localtime()\u51fd\u6570\u5c06\u5f53\u524d\u65f6\u95f4\u8f6c\u6362\u4e3a\u4e00\u4e2a\u7ed3\u6784\u5316\u65f6\u95f4\u5bf9\u8c61\uff0c\u5e76\u4ece\u4e2d\u63d0\u53d6\u65f6\u5206\u79d2\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u63d0\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u65f6\u5206\u79d2\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u7ed3\u6784\u5316\u65f6\u95f4\u5bf9\u8c61<\/p>\n<p>local_time = time.localtime(current_time)<\/p>\n<h2><strong>\u63d0\u53d6\u65f6\u3001\u5206\u3001\u79d2<\/strong><\/h2>\n<p>hour = local_time.tm_hour<\/p>\n<p>minute = local_time.tm_min<\/p>\n<p>second = local_time.tm_sec<\/p>\n<p>print(f&quot;\u5f53\u524d\u65f6\u95f4\u662f\uff1a{hour}\u65f6 {minute}\u5206 {second}\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528time.localtime()\u51fd\u6570\u5c06\u5f53\u524d\u65f6\u95f4\u8f6c\u6362\u4e3a\u4e00\u4e2a\u7ed3\u6784\u5316\u65f6\u95f4\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528tm_hour\u3001tm_min\u548ctm_sec\u5c5e\u6027\u63d0\u53d6\u65f6\u95f4\u7684\u5404\u4e2a\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h3>\u683c\u5f0f\u5316\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5c06\u65e5\u671f\u65f6\u95f4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u683c\u5f0f\uff0c\u5e76\u4ece\u4e2d\u63d0\u53d6\u65f6\u5206\u79d2\u6570\u636e\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528time\u6a21\u5757\u7684strftime()\u51fd\u6570\u6765\u683c\u5f0f\u5316\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u683c\u5f0f\u5316\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32\u5e76\u63d0\u53d6\u65f6\u5206\u79d2\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u7ed3\u6784\u5316\u65f6\u95f4\u5bf9\u8c61<\/p>\n<p>local_time = time.localtime(current_time)<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>datetime_str = time.strftime(&#39;%Y-%m-%d %H:%M:%S&#39;, local_time)<\/p>\n<p>print(datetime_str)<\/p>\n<h2><strong>\u63d0\u53d6\u65f6\u3001\u5206\u3001\u79d2<\/strong><\/h2>\n<p>hour = local_time.tm_hour<\/p>\n<p>minute = local_time.tm_min<\/p>\n<p>second = local_time.tm_sec<\/p>\n<p>print(f&quot;\u5f53\u524d\u65f6\u95f4\u662f\uff1a{hour}\u65f6 {minute}\u5206 {second}\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528time.strftime()\u51fd\u6570\u5c06\u5f53\u524d\u65f6\u95f4\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32\uff0c\u5e76\u4f7f\u7528tm_hour\u3001tm_min\u548ctm_sec\u5c5e\u6027\u63d0\u53d6\u65f6\u95f4\u7684\u5404\u4e2a\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e09\u3001\u4f7f\u7528PANDAS\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u63d0\u4f9b\u4e86\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u6570\u636e\u7684\u529f\u80fd\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528pandas\u6765\u521b\u5efa\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61\uff0c\u5e76\u4ece\u4e2d\u63d0\u53d6\u65f6\u5206\u79d2\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u521b\u5efa\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528pandas\u7684to_datetime()\u51fd\u6570\u6765\u521b\u5efa\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u521b\u5efa\u5f53\u524d\u65f6\u95f4\u7684\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>now = pd.to_datetime(&#39;now&#39;)<\/p>\n<p>print(now)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528pd.to_datetime(&#39;now&#39;)\u51fd\u6570\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u53d8\u91cfnow\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u63d0\u53d6\u65f6\u5206\u79d2\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u4e00\u65e6\u4f60\u6709\u4e86\u4e00\u4e2a\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u5176\u5185\u7f6e\u7684\u65b9\u6cd5\u6765\u63d0\u53d6\u65f6\u5206\u79d2\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u63d0\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u65f6\u5206\u79d2\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u63d0\u53d6\u65f6\u3001\u5206\u3001\u79d2<\/p>\n<p>hour = now.hour<\/p>\n<p>minute = now.minute<\/p>\n<p>second = now.second<\/p>\n<p>print(f&quot;\u5f53\u524d\u65f6\u95f4\u662f\uff1a{hour}\u65f6 {minute}\u5206 {second}\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528hour\u3001minute\u548csecond\u5c5e\u6027\u6765\u63d0\u53d6\u65f6\u95f4\u7684\u5404\u4e2a\u90e8\u5206\uff0c\u5e76\u5c06\u5176\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u89e3\u6790\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u4ece\u5b57\u7b26\u4e32\u4e2d\u89e3\u6790\u65e5\u671f\u65f6\u95f4\u4fe1\u606f\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528pandas\u7684to_datetime()\u51fd\u6570\u6765\u89e3\u6790\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u89e3\u6790\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32\u5e76\u63d0\u53d6\u65f6\u5206\u79d2\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u89e3\u6790\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32<\/p>\n<p>datetime_str = &quot;2023-10-05 14:30:45&quot;<\/p>\n<p>datetime_obj = pd.to_datetime(datetime_str)<\/p>\n<h2><strong>\u63d0\u53d6\u65f6\u3001\u5206\u3001\u79d2<\/strong><\/h2>\n<p>hour = datetime_obj.hour<\/p>\n<p>minute = datetime_obj.minute<\/p>\n<p>second = datetime_obj.second<\/p>\n<p>print(f&quot;\u89e3\u6790\u540e\u7684\u65f6\u95f4\u662f\uff1a{hour}\u65f6 {minute}\u5206 {second}\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528pd.to_datetime()\u51fd\u6570\u89e3\u6790\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32\uff0c\u5e76\u4f7f\u7528hour\u3001minute\u548csecond\u5c5e\u6027\u63d0\u53d6\u65f6\u95f4\u7684\u5404\u4e2a\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h3>\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e<\/h3>\n<\/p>\n<p><p>pandas\u8fd8\u63d0\u4f9b\u4e86\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u7684\u529f\u80fd\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528pandas\u7684date_range()\u51fd\u6570\u6765\u521b\u5efa\u65f6\u95f4\u5e8f\u5217\uff0c\u5e76\u4ece\u4e2d\u63d0\u53d6\u65f6\u5206\u79d2\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u521b\u5efa\u65f6\u95f4\u5e8f\u5217\u5e76\u63d0\u53d6\u65f6\u5206\u79d2\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u65f6\u95f4\u5e8f\u5217<\/p>\n<p>time_series = pd.date_range(start=&#39;2023-10-05 14:30:45&#39;, periods=5, freq=&#39;H&#39;)<\/p>\n<p>print(time_series)<\/p>\n<h2><strong>\u63d0\u53d6\u65f6\u3001\u5206\u3001\u79d2<\/strong><\/h2>\n<p>hours = time_series.hour<\/p>\n<p>minutes = time_series.minute<\/p>\n<p>seconds = time_series.second<\/p>\n<p>print(f&quot;\u65f6\u95f4\u5e8f\u5217\u7684\u5c0f\u65f6\u90e8\u5206\uff1a{hours}&quot;)<\/p>\n<p>print(f&quot;\u65f6\u95f4\u5e8f\u5217\u7684\u5206\u949f\u90e8\u5206\uff1a{minutes}&quot;)<\/p>\n<p>print(f&quot;\u65f6\u95f4\u5e8f\u5217\u7684\u79d2\u90e8\u5206\uff1a{seconds}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528pd.date_range()\u51fd\u6570\u521b\u5efa\u65f6\u95f4\u5e8f\u5217\uff0c\u5e76\u4f7f\u7528hour\u3001minute\u548csecond\u5c5e\u6027\u63d0\u53d6\u65f6\u95f4\u7684\u5404\u4e2a\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><p><strong>\u56db\u3001\u4f7f\u7528REGEX\uff08\u6b63\u5219\u8868\u8fbe\u5f0f\uff09<\/strong><\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\uff08regular expression\uff0c\u7b80\u79f0regex\uff09\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u5339\u914d\u548c\u641c\u7d22\u5de5\u5177\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u65f6\u5206\u79d2\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u5339\u914d\u65f6\u95f4\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u65f6\u95f4\u5b57\u7b26\u4e32\uff0c\u5e76\u4ece\u4e2d\u63d0\u53d6\u65f6\u5206\u79d2\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u65f6\u95f4\u5b57\u7b26\u4e32\u5e76\u63d0\u53d6\u65f6\u5206\u79d2\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u5b9a\u4e49\u65f6\u95f4\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>time_str = &quot;14:30:45&quot;<\/p>\n<h2><strong>\u5b9a\u4e49\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f<\/strong><\/h2>\n<p>pattern = r&#39;(\\d{2}):(\\d{2}):(\\d{2})&#39;<\/p>\n<h2><strong>\u5339\u914d\u65f6\u95f4\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>match = re.match(pattern, time_str)<\/p>\n<h2><strong>\u63d0\u53d6\u65f6\u3001\u5206\u3001\u79d2<\/strong><\/h2>\n<p>if match:<\/p>\n<p>    hour = int(match.group(1))<\/p>\n<p>    minute = int(match.group(2))<\/p>\n<p>    second = int(match.group(3))<\/p>\n<p>    print(f&quot;\u5339\u914d\u5230\u7684\u65f6\u95f4\u662f\uff1a{hour}\u65f6 {minute}\u5206 {second}\u79d2&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u672a\u5339\u914d\u5230\u65f6\u95f4\u5b57\u7b26\u4e32&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u6765\u5339\u914d\u65f6\u95f4\u5b57\u7b26\u4e32\uff0c\u5e76\u4f7f\u7528re.match()\u51fd\u6570\u8fdb\u884c\u5339\u914d\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528group()\u65b9\u6cd5\u63d0\u53d6\u5339\u914d\u5230\u7684\u65f6\u5206\u79d2\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u89e3\u6790\u590d\u6742\u65f6\u95f4\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u65f6\u95f4\u5b57\u7b26\u4e32\u53ef\u80fd\u5305\u542b\u5176\u4ed6\u6587\u672c\u4fe1\u606f\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u89e3\u6790\u590d\u6742\u7684\u65f6\u95f4\u5b57\u7b26\u4e32\uff0c\u5e76\u4ece\u4e2d\u63d0\u53d6\u65f6\u5206\u79d2\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u89e3\u6790\u590d\u6742\u7684\u65f6\u95f4\u5b57\u7b26\u4e32\u5e76\u63d0\u53d6\u65f6\u5206\u79d2\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u590d\u6742\u65f6\u95f4\u5b57\u7b26\u4e32<\/p>\n<p>complex_time_str = &quot;The event starts at 14:30:45 and ends at 18:45:30&quot;<\/p>\n<h2><strong>\u5b9a\u4e49\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f<\/strong><\/h2>\n<p>pattern = r&#39;(\\d{2}):(\\d{2}):(\\d{2})&#39;<\/p>\n<h2><strong>\u5339\u914d\u6240\u6709\u65f6\u95f4\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>matches = re.findall(pattern, complex_time_str)<\/p>\n<h2><strong>\u63d0\u53d6\u65f6\u3001\u5206\u3001\u79d2<\/strong><\/h2>\n<p>for match in matches:<\/p>\n<p>    hour = int(match[0])<\/p>\n<p>    minute = int(match[1])<\/p>\n<p>    second = int(match[2])<\/p>\n<p>    print(f&quot;\u5339\u914d\u5230\u7684\u65f6\u95f4\u662f\uff1a{hour}\u65f6 {minute}\u5206 {second}\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u6765\u5339\u914d\u6240\u6709\u65f6\u95f4\u5b57\u7b26\u4e32\uff0c\u5e76\u4f7f\u7528re.findall()\u51fd\u6570\u8fdb\u884c\u5339\u914d\u3002\u7136\u540e\uff0c\u6211\u4eec\u904d\u5386\u5339\u914d\u7ed3\u679c\uff0c\u63d0\u53d6\u65f6\u5206\u79d2\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u81ea\u5b9a\u4e49\u65f6\u95f4\u683c\u5f0f<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u81ea\u5b9a\u4e49\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u4ee5\u5339\u914d\u4e0d\u540c\u7684\u65f6\u95f4\u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u81ea\u5b9a\u4e49\u65f6\u95f4\u683c\u5f0f\u5e76\u63d0\u53d6\u65f6\u5206\u79d2\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u81ea\u5b9a\u4e49\u65f6\u95f4\u5b57\u7b26\u4e32<\/p>\n<p>custom_time_str = &quot;14-30-45&quot;<\/p>\n<h2><strong>\u5b9a\u4e49\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f<\/strong><\/h2>\n<p>pattern = r&#39;(\\d{2})-(\\d{2})-(\\d{2})&#39;<\/p>\n<h2><strong>\u5339\u914d\u65f6\u95f4\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>match = re.match(pattern, custom_time_str)<\/p>\n<h2><strong>\u63d0\u53d6\u65f6\u3001\u5206\u3001\u79d2<\/strong><\/h2>\n<p>if match:<\/p>\n<p>    hour = int(match.group(1))<\/p>\n<p>    minute = int(match.group(2))<\/p>\n<p>    second = int(match.group(3))<\/p>\n<p>    print(f&quot;\u5339\u914d\u5230\u7684\u65f6\u95f4\u662f\uff1a{hour}\u65f6 {minute}\u5206 {second}\u79d2&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u672a\u5339\u914d\u5230\u65f6\u95f4\u5b57\u7b26\u4e32&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u65f6\u95f4\u683c\u5f0f\uff0c\u5e76\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u8fdb\u884c\u5339\u914d\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528group()\u65b9\u6cd5\u63d0\u53d6\u5339\u914d\u5230\u7684\u65f6\u5206\u79d2\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u4f60\u53ef\u4ee5\u7075\u6d3b\u5730\u4ece\u5404\u79cd\u683c\u5f0f\u7684\u65f6\u95f4\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u65f6\u5206\u79d2\u6570\u636e\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6587\u672c\u5339\u914d\u548c\u641c\u7d22\u529f\u80fd\uff0c\u4f7f\u4f60\u80fd\u591f\u8f7b\u677e\u5730\u5904\u7406\u590d\u6742\u7684\u65f6\u95f4\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5728Python\u4e2d\u63d0\u53d6\u65f6\u5206\u79d2\u6570\u636e\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528datetime\u6a21\u5757\u3001time\u6a21\u5757\u3001pandas\u6a21\u5757\u4ee5\u53ca\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5904\u7406\u548c\u63d0\u53d6\u65f6\u5206\u79d2\u6570\u636e\uff0c\u4e3a\u4f60\u7684\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u5de5\u4f5c\u63d0\u4f9b\u6709\u529b\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u63d0\u53d6\u65f6\u5206\u79d2\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\u63d0\u53d6\u65f6\u5206\u79d2\u6570\u636e\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>datetime<\/code>\u6a21\u5757\u3002\u9996\u5148\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>strptime<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u683c\u5f0f\u7684\u65f6\u95f4\u8f6c\u6362\u4e3a<code>datetime<\/code>\u5bf9\u8c61\u3002\u4e4b\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>hour<\/code>\u3001<code>minute<\/code>\u548c<code>second<\/code>\u5c5e\u6027\u83b7\u53d6\u65f6\u5206\u79d2\u7684\u5177\u4f53\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from datetime import datetime\n\ntime_str = &quot;2023-10-05 14:30:45&quot;\ntime_obj = datetime.strptime(time_str, &quot;%Y-%m-%d %H:%M:%S&quot;)\n\nhours = time_obj.hour\nminutes = time_obj.minute\nseconds = time_obj.second\n\nprint(hours, minutes, seconds)  # \u8f93\u51fa: 14 30 45\n<\/code><\/pre>\n<p><strong>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u5305\u542b\u65f6\u5206\u79d2\u7684\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u5904\u7406\u5305\u542b\u65f6\u5206\u79d2\u7684\u5b57\u7b26\u4e32\u901a\u5e38\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a<code>datetime<\/code>\u683c\u5f0f\uff0c\u4ee5\u4fbf\u8fdb\u884c\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u3002\u4f7f\u7528<code>pandas<\/code>\u5e93\u7684<code>to_datetime<\/code>\u51fd\u6570\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ntime_series = pd.Series([&#39;2023-10-05 14:30:45&#39;, &#39;2023-10-06 15:45:30&#39;])\ntime_series = pd.to_datetime(time_series)\n\n# \u63d0\u53d6\u65f6\u5206\u79d2\nhours = time_series.dt.hour\nminutes = time_series.dt.minute\nseconds = time_series.dt.second\n\nprint(hours, minutes, seconds)\n<\/code><\/pre>\n<p><strong>\u53ef\u4ee5\u63d0\u53d6\u54ea\u4e9b\u683c\u5f0f\u7684\u65f6\u5206\u79d2\u6570\u636e\uff1f<\/strong><br \/>Python\u7684<code>datetime<\/code>\u6a21\u5757\u548c<code>pandas<\/code>\u5e93\u652f\u6301\u591a\u79cd\u65f6\u95f4\u683c\u5f0f\u7684\u63d0\u53d6\u3002\u5e38\u89c1\u7684\u683c\u5f0f\u5305\u62ecISO 8601\u6807\u51c6\uff08\u5982<code>YYYY-MM-DD HH:MM:SS<\/code>\uff09\u3001\u81ea\u5b9a\u4e49\u683c\u5f0f\uff08\u5982<code>%d\/%m\/%Y %H:%M:%S<\/code>\uff09\u7b49\u3002\u7528\u6237\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u4ee5\u4fbf\u987a\u5229\u63d0\u53d6\u65f6\u5206\u79d2\u6570\u636e\u3002\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528<code>strptime<\/code>\u548c<code>to_datetime<\/code>\uff0c\u51e0\u4e4e\u6240\u6709\u5e38\u89c1\u7684\u65f6\u95f4\u683c\u5f0f\u90fd\u53ef\u4ee5\u88ab\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u65f6\u5206\u79d2\u6570\u636e\u63d0\u53d6\u7684\u65b9\u5f0f\u5305\u62ec\uff1a\u4f7f\u7528datetime\u6a21\u5757\u3001\u4f7f\u7528time\u6a21\u5757\u3001\u4f7f\u7528pandas\u6a21\u5757 \u5728Python\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1082559,"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\/1082551"}],"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=1082551"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1082551\/revisions"}],"predecessor-version":[{"id":1082563,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1082551\/revisions\/1082563"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1082559"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1082551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1082551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1082551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}