{"id":959558,"date":"2024-12-27T03:41:47","date_gmt":"2024-12-26T19:41:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/959558.html"},"modified":"2024-12-27T03:41:49","modified_gmt":"2024-12-26T19:41:49","slug":"python%e5%a6%82%e4%bd%95%e6%98%be%e7%a4%ba%e4%b8%ad%e6%96%87%e6%97%b6%e9%97%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/959558.html","title":{"rendered":"python\u5982\u4f55\u663e\u793a\u4e2d\u6587\u65f6\u95f4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102339\/6be493b7-e602-4ad0-ba7e-03f683e0dce4.webp\" alt=\"python\u5982\u4f55\u663e\u793a\u4e2d\u6587\u65f6\u95f4\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u663e\u793a\u4e2d\u6587\u65f6\u95f4\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528datetime\u6a21\u5757\u3001locale\u6a21\u5757\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6elocale\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7b49\u65b9\u6cd5\u5b9e\u73b0\u4e2d\u6587\u65f6\u95f4\u663e\u793a\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u89e3\u91ca\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u5728Python\u4e2d\u663e\u793a\u4e2d\u6587\u65f6\u95f4\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u7cfb\u7edf\u652f\u6301\u4e2d\u6587\u8bed\u8a00\u73af\u5883\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6elocale\u6a21\u5757\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002locale\u6a21\u5757\u5141\u8bb8\u7a0b\u5e8f\u6839\u636e\u4e0d\u540c\u7684\u8bed\u8a00\u73af\u5883\u6765\u683c\u5f0f\u5316\u65e5\u671f\u3001\u65f6\u95f4\u3001\u6570\u5b57\u7b49\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e00\u3001\u4f7f\u7528DATETIME\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>Python\u7684datetime\u6a21\u5757\u63d0\u4f9b\u4e86\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u51fd\u6570\u548c\u7c7b\u3002\u8981\u663e\u793a\u4e2d\u6587\u65f6\u95f4\uff0c\u9996\u5148\u9700\u8981\u683c\u5f0f\u5316datetime\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import datetime<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>now = datetime.datetime.now()<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u65f6\u95f4\u4e3a\u4e2d\u6587\u683c\u5f0f<\/strong><\/h2>\n<p>chinese_time = now.strftime(&#39;%Y\u5e74%m\u6708%d\u65e5 %H:%M:%S&#39;)<\/p>\n<p>print(chinese_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cstrftime\u51fd\u6570\u7528\u4e8e\u683c\u5f0f\u5316\u65e5\u671f\u65f6\u95f4\uff0c\u683c\u5f0f\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u4e86\u4e2d\u6587\u5b57\u7b26\u201c\u5e74\u201d\u3001\u201c\u6708\u201d\u3001\u201c\u65e5\u201d\u7b49\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u4e2d\u6587\u65f6\u95f4\u7684\u663e\u793a\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u4f7f\u7528LOCALE\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>locale\u6a21\u5757\u5141\u8bb8\u7a0b\u5e8f\u6839\u636e\u4e0d\u540c\u7684\u8bed\u8a00\u73af\u5883\u6765\u683c\u5f0f\u5316\u65e5\u671f\u3001\u65f6\u95f4\u7b49\u3002\u5728\u663e\u793a\u4e2d\u6587\u65f6\u95f4\u65f6\uff0clocale\u6a21\u5757\u53ef\u4ee5\u7528\u4e8e\u8bbe\u7f6e\u8bed\u8a00\u73af\u5883\u4e3a\u4e2d\u6587\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import locale<\/p>\n<p>import datetime<\/p>\n<h2><strong>\u8bbe\u7f6e\u8bed\u8a00\u73af\u5883\u4e3a\u4e2d\u6587<\/strong><\/h2>\n<p>locale.setlocale(locale.LC_TIME, &#39;zh_CN.UTF-8&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>now = datetime.datetime.now()<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u65f6\u95f4\u4e3a\u4e2d\u6587\u683c\u5f0f<\/strong><\/h2>\n<p>chinese_time = now.strftime(&#39;%Y\u5e74%m\u6708%d\u65e5 %A %H:%M:%S&#39;)<\/p>\n<p>print(chinese_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5c06\u8bed\u8a00\u73af\u5883\u8bbe\u7f6e\u4e3a\u201czh_CN.UTF-8\u201d\uff0cstrftime\u51fd\u6570\u5c06\u81ea\u52a8\u4f7f\u7528\u4e2d\u6587\u683c\u5f0f\u6765\u663e\u793a\u65e5\u671f\u548c\u65f6\u95f4\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0clocale\u8bbe\u7f6e\u53ef\u80fd\u4f1a\u53d7\u5230\u64cd\u4f5c\u7cfb\u7edf\u914d\u7f6e\u7684\u5f71\u54cd\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\u5b83\u4e5f\u63d0\u4f9b\u4e86\u65f6\u95f4\u5e8f\u5217\u5904\u7406\u7684\u529f\u80fd\u3002\u5728\u663e\u793a\u4e2d\u6587\u65f6\u95f4\u65f6\uff0cPandas\u53ef\u4ee5\u901a\u8fc7strftime\u51fd\u6570\u548c\u81ea\u5b9a\u4e49\u683c\u5f0f\u5b57\u7b26\u4e32\u6765\u5b9e\u73b0\u3002<\/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.Timestamp.now()<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u65f6\u95f4\u4e3a\u4e2d\u6587\u683c\u5f0f<\/strong><\/h2>\n<p>chinese_time = now.strftime(&#39;%Y\u5e74%m\u6708%d\u65e5 %A %H:%M:%S&#39;)<\/p>\n<p>print(chinese_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Pandas\u7684Timestamp\u5bf9\u8c61\u652f\u6301strftime\u51fd\u6570\uff0c\u56e0\u6b64\u53ef\u4ee5\u4f7f\u7528\u4e0edatetime\u6a21\u5757\u7c7b\u4f3c\u7684\u65b9\u6cd5\u8fdb\u884c\u683c\u5f0f\u5316\u3002<\/p>\n<\/p>\n<p><p><strong>\u56db\u3001\u4f7f\u7528\u65f6\u95f4\u6233\u8f6c\u5316<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u573a\u666f\u4e2d\uff0c\u65f6\u95f4\u6570\u636e\u53ef\u80fd\u4ee5\u65f6\u95f4\u6233\u7684\u5f62\u5f0f\u5b58\u5728\u3002\u53ef\u4ee5\u901a\u8fc7\u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61\uff0c\u7136\u540e\u8fdb\u884c\u683c\u5f0f\u5316\u6765\u5b9e\u73b0\u4e2d\u6587\u65f6\u95f4\u7684\u663e\u793a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import datetime<\/p>\n<h2><strong>\u65f6\u95f4\u6233<\/strong><\/h2>\n<p>timestamp = 1672531199<\/p>\n<h2><strong>\u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61<\/strong><\/h2>\n<p>dt_object = datetime.datetime.fromtimestamp(timestamp)<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u65f6\u95f4\u4e3a\u4e2d\u6587\u683c\u5f0f<\/strong><\/h2>\n<p>chinese_time = dt_object.strftime(&#39;%Y\u5e74%m\u6708%d\u65e5 %H:%M:%S&#39;)<\/p>\n<p>print(chinese_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528strftime\u51fd\u6570\u8fdb\u884c\u683c\u5f0f\u5316\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e2d\u6587\u65f6\u95f4\u7684\u663e\u793a\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e94\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u66f4\u7075\u6d3b\u5730\u663e\u793a\u4e2d\u6587\u65f6\u95f4\u3002\u53ef\u4ee5\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u6839\u636e\u9700\u8981\u683c\u5f0f\u5316\u65f6\u95f4\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import datetime<\/p>\n<p>def custom_chinese_time_format(dt):<\/p>\n<p>    # \u81ea\u5b9a\u4e49\u683c\u5f0f\u5316\u51fd\u6570<\/p>\n<p>    return f&quot;{dt.year}\u5e74{dt.month}\u6708{dt.day}\u65e5 {dt.hour}\u65f6{dt.minute}\u5206{dt.second}\u79d2&quot;<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>now = datetime.datetime.now()<\/p>\n<h2><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u683c\u5f0f\u5316\u51fd\u6570<\/strong><\/h2>\n<p>chinese_time = custom_chinese_time_format(now)<\/p>\n<p>print(chinese_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u81ea\u5b9a\u4e49\u683c\u5f0f\u5316\u51fd\u6570\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u8c03\u6574\u65f6\u95f4\u663e\u793a\u7684\u683c\u5f0f\u548c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u663e\u793a\u4e2d\u6587\u65f6\u95f4\u4e3b\u8981\u4f9d\u8d56\u4e8edatetime\u6a21\u5757\u3001locale\u6a21\u5757\u548c\u81ea\u5b9a\u4e49\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3002\u901a\u8fc7\u9002\u5f53\u8bbe\u7f6e\u8bed\u8a00\u73af\u5883\u548c\u683c\u5f0f\u5316\u9009\u9879\uff0c\u53ef\u4ee5\u5b9e\u73b0\u591a\u79cd\u5f62\u5f0f\u7684\u4e2d\u6587\u65f6\u95f4\u663e\u793a\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u548c\u6a21\u5757\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u6ee1\u8db3\u9879\u76ee\u7684\u8981\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u683c\u5f0f\u5316\u4e2d\u6587\u65f6\u95f4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u6765\u83b7\u53d6\u548c\u683c\u5f0f\u5316\u65f6\u95f4\u3002\u4e3a\u4e86\u663e\u793a\u4e2d\u6587\u65f6\u95f4\uff0c\u53ef\u4ee5\u5229\u7528<code>strftime<\/code>\u65b9\u6cd5\u81ea\u5b9a\u4e49\u65f6\u95f4\u683c\u5f0f\uff0c\u5e76\u7ed3\u5408\u4e2d\u6587\u5b57\u7b26\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u7528<code>now.strftime(&quot;%Y\u5e74%m\u6708%d\u65e5 %H\u65f6%M\u5206%S\u79d2&quot;)<\/code>\u6765\u8f93\u51fa\u4e2d\u6587\u683c\u5f0f\u7684\u65f6\u95f4\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u5904\u7406\u65f6\u533a\u4ee5\u663e\u793a\u4e2d\u6587\u65f6\u95f4\uff1f<\/strong><br \/>\u5904\u7406\u65f6\u533a\u9700\u8981\u4f7f\u7528<code>pytz<\/code>\u5e93\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u4f60\u6b63\u786e\u5730\u8f6c\u6362\u65f6\u533a\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86<code>pytz<\/code>\uff0c\u7136\u540e\u901a\u8fc7<code>datetime<\/code>\u6a21\u5757\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u5e76\u4f7f\u7528<code>astimezone<\/code>\u65b9\u6cd5\u8f6c\u6362\u4e3a\u6240\u9700\u7684\u65f6\u533a\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u663e\u793a\u7684\u65f6\u95f4\u662f\u7b26\u5408\u5f53\u5730\u6807\u51c6\u7684\u4e2d\u6587\u65f6\u95f4\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u65f6\u95f4\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\u5e76\u663e\u793a\u4e3a\u4e2d\u6587\u683c\u5f0f\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u4e2d\u7684<code>timestamp()<\/code>\u65b9\u6cd5\u5c06\u65f6\u95f4\u5bf9\u8c61\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\u3002\u901a\u8fc7\u5c06\u65f6\u95f4\u6233\u518d\u6b21\u8f6c\u6362\u4e3a\u65f6\u95f4\u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528<code>strftime<\/code>\u683c\u5f0f\u5316\u8f93\u51fa\u4e3a\u4e2d\u6587\u65f6\u95f4\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u5148\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u65f6\u95f4\u6233\uff0c\u518d\u5c06\u5176\u8f6c\u6362\u4e3a<code>datetime<\/code>\u5bf9\u8c61\u5e76\u663e\u793a\u4e3a\u4e2d\u6587\u683c\u5f0f\u3002\u8fd9\u6837\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u5904\u7406\u65f6\u95f4\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u663e\u793a\u4e2d\u6587\u65f6\u95f4\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528datetime\u6a21\u5757\u3001locale\u6a21\u5757\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u53ef\u4ee5 [&hellip;]","protected":false},"author":3,"featured_media":959565,"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\/959558"}],"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=959558"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/959558\/revisions"}],"predecessor-version":[{"id":959568,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/959558\/revisions\/959568"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/959565"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=959558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=959558"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=959558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}