{"id":1096768,"date":"2025-01-08T15:04:45","date_gmt":"2025-01-08T07:04:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1096768.html"},"modified":"2025-01-08T15:04:48","modified_gmt":"2025-01-08T07:04:48","slug":"python%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87%e7%a7%92%e6%95%b0%e6%b1%82%e6%97%b6%e5%88%86%e7%a7%92-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1096768.html","title":{"rendered":"python\u5982\u4f55\u901a\u8fc7\u79d2\u6570\u6c42\u65f6\u5206\u79d2"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211822\/f2ab0a06-a32d-48be-ac48-5769b69180d6.webp\" alt=\"python\u5982\u4f55\u901a\u8fc7\u79d2\u6570\u6c42\u65f6\u5206\u79d2\" \/><\/p>\n<p><p> <strong>Python\u901a\u8fc7\u79d2\u6570\u6c42\u65f6\u5206\u79d2\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u6574\u9664\u548c\u53d6\u4f59\u8fd0\u7b97\u3001\u4f7f\u7528\u5185\u7f6e\u5e93datetime\u3001\u4f7f\u7528divmod\u51fd\u6570\u7b49\u3002<\/strong>\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u4f8b\u5982\u6574\u9664\u548c\u53d6\u4f59\u8fd0\u7b97\u7075\u6d3b\u6027\u9ad8\u3001datetime\u5e93\u529f\u80fd\u5f3a\u5927\u3001divmod\u51fd\u6570\u7b80\u6d01\u9ad8\u6548\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u6574\u9664\u548c\u53d6\u4f59\u8fd0\u7b97<\/strong>\uff1a\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u7528\u7b80\u5355\u7684\u6570\u5b66\u8fd0\u7b97\u6765\u5c06\u79d2\u6570\u8f6c\u6362\u4e3a\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u3002\u5177\u4f53\u6b65\u9aa4\u662f\u5148\u5c06\u79d2\u6570\u6574\u96643600\u5f97\u5230\u5c0f\u65f6\u6570\uff0c\u7136\u540e\u7528\u53d6\u4f59\u8fd0\u7b97\u5f97\u5230\u5269\u4f59\u79d2\u6570\uff0c\u518d\u5c06\u5269\u4f59\u79d2\u6570\u6574\u966460\u5f97\u5230\u5206\u949f\u6570\uff0c\u6700\u540e\u53d6\u4f59\u8fd0\u7b97\u5f97\u5230\u79d2\u6570\u3002\u6b64\u65b9\u6cd5\u7075\u6d3b\u6027\u9ad8\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u9002\u5e94\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u6574\u9664\u548c\u53d6\u4f59\u8fd0\u7b97<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u6574\u9664\u548c\u53d6\u4f59\u8fd0\u7b97\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u603b\u79d2\u6570\u8f6c\u6362\u4e3a\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4f9d\u8d56\u989d\u5916\u7684\u5e93\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u79d2\u6570\u8f6c\u6362\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def convert_seconds(seconds):<\/p>\n<p>    hours = seconds \/\/ 3600<\/p>\n<p>    minutes = (seconds % 3600) \/\/ 60<\/p>\n<p>    rem<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ning_seconds = seconds % 60<\/p>\n<p>    return hours, minutes, remaining_seconds<\/p>\n<p>seconds = 3661<\/p>\n<p>hours, minutes, remaining_seconds = convert_seconds(seconds)<\/p>\n<p>print(f&quot;{hours}\u5c0f\u65f6, {minutes}\u5206\u949f, {remaining_seconds}\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c3661\u79d2\u88ab\u8f6c\u6362\u4e3a1\u5c0f\u65f61\u5206\u949f1\u79d2\u3002\u8fd9\u79cd\u65b9\u6cd5\u76f4\u89c2\u4e14\u6613\u4e8e\u7406\u89e3\uff0c\u4f46\u9700\u8981\u624b\u52a8\u5904\u7406\u6bcf\u4e2a\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528datetime\u5e93<\/h2>\n<\/p>\n<p><p>Python\u7684datetime\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u65f6\u95f4\u5904\u7406\u529f\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5b83\u6765\u8fdb\u884c\u79d2\u6570\u8f6c\u6362\u3002datetime\u5e93\u4e2d\u7684timedelta\u7c7b\u53ef\u4ee5\u65b9\u4fbf\u5730\u8868\u793a\u65f6\u95f4\u6bb5\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65b9\u6cd5\u6765\u8fdb\u884c\u65f6\u95f4\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import timedelta<\/p>\n<p>def convert_seconds_with_datetime(seconds):<\/p>\n<p>    delta = timedelta(seconds=seconds)<\/p>\n<p>    hours = delta.seconds \/\/ 3600<\/p>\n<p>    minutes = (delta.seconds % 3600) \/\/ 60<\/p>\n<p>    remaining_seconds = delta.seconds % 60<\/p>\n<p>    return delta.days, hours, minutes, remaining_seconds<\/p>\n<p>seconds = 90061<\/p>\n<p>days, hours, minutes, remaining_seconds = convert_seconds_with_datetime(seconds)<\/p>\n<p>print(f&quot;{days}\u5929, {hours}\u5c0f\u65f6, {minutes}\u5206\u949f, {remaining_seconds}\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528datetime\u5e93\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u5904\u7406\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\uff0c\u8fd8\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u5929\u6570\u3002\u8fd9\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u8f83\u957f\u65f6\u95f4\u6bb5\u7684\u60c5\u51b5\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528divmod\u51fd\u6570<\/h2>\n<\/p>\n<p><p>divmod\u51fd\u6570\u53ef\u4ee5\u540c\u65f6\u8fd4\u56de\u5546\u548c\u4f59\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u8fd9\u4e00\u7279\u6027\u6765\u7b80\u5316\u79d2\u6570\u8f6c\u6362\u7684\u4ee3\u7801\u3002divmod\u51fd\u6570\u53ef\u4ee5\u51cf\u5c11\u4ee3\u7801\u7684\u5197\u4f59\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def convert_seconds_with_divmod(seconds):<\/p>\n<p>    hours, remainder = divmod(seconds, 3600)<\/p>\n<p>    minutes, remaining_seconds = divmod(remainder, 60)<\/p>\n<p>    return hours, minutes, remaining_seconds<\/p>\n<p>seconds = 3661<\/p>\n<p>hours, minutes, remaining_seconds = convert_seconds_with_divmod(seconds)<\/p>\n<p>print(f&quot;{hours}\u5c0f\u65f6, {minutes}\u5206\u949f, {remaining_seconds}\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cdivmod\u51fd\u6570\u88ab\u7528\u4e8e\u8ba1\u7b97\u5c0f\u65f6\u548c\u5206\u949f\uff0c\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u660e\u4e86\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93humanize<\/h2>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u65f6\u95f4\u8f6c\u6362\u9700\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93humanize\u3002humanize\u5e93\u63d0\u4f9b\u4e86\u4eba\u6027\u5316\u7684\u65f6\u95f4\u8868\u793a\u65b9\u6cd5\uff0c\u4f7f\u65f6\u95f4\u8f6c\u6362\u7ed3\u679c\u66f4\u52a0\u81ea\u7136\u548c\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import humanize<\/p>\n<p>from datetime import timedelta<\/p>\n<p>def convert_seconds_with_humanize(seconds):<\/p>\n<p>    delta = timedelta(seconds=seconds)<\/p>\n<p>    return humanize.precisedelta(delta)<\/p>\n<p>seconds = 3661<\/p>\n<p>humanized_time = convert_seconds_with_humanize(seconds)<\/p>\n<p>print(humanized_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c3661\u79d2\u88ab\u8f6c\u6362\u4e3a\u201c1 hour, 1 minute and 1 second\u201d\uff0c\u8fd9\u79cd\u8868\u793a\u65b9\u6cd5\u66f4\u52a0\u81ea\u7136\uff0c\u9002\u5408\u5c55\u793a\u7ed9\u7528\u6237\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u5904\u7406\u8d1f\u6570\u79d2\u6570<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u8d1f\u6570\u79d2\u6570\u7684\u60c5\u51b5\u3002\u6211\u4eec\u9700\u8981\u5728\u8f6c\u6362\u65f6\u5904\u7406\u8d1f\u6570\u79d2\u6570\uff0c\u4ee5\u786e\u4fdd\u7ed3\u679c\u6b63\u786e\u3002\u53ef\u4ee5\u901a\u8fc7\u7edd\u5bf9\u503c\u51fd\u6570abs\u6765\u5904\u7406\u8d1f\u6570\u79d2\u6570\uff0c\u5e76\u5728\u6700\u7ec8\u7ed3\u679c\u4e2d\u52a0\u4e0a\u8d1f\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def convert_seconds_with_negatives(seconds):<\/p>\n<p>    is_negative = seconds &lt; 0<\/p>\n<p>    seconds = abs(seconds)<\/p>\n<p>    hours, remainder = divmod(seconds, 3600)<\/p>\n<p>    minutes, remaining_seconds = divmod(remainder, 60)<\/p>\n<p>    if is_negative:<\/p>\n<p>        return -hours, -minutes, -remaining_seconds<\/p>\n<p>    return hours, minutes, remaining_seconds<\/p>\n<p>seconds = -3661<\/p>\n<p>hours, minutes, remaining_seconds = convert_seconds_with_negatives(seconds)<\/p>\n<p>print(f&quot;{hours}\u5c0f\u65f6, {minutes}\u5206\u949f, {remaining_seconds}\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c-3661\u79d2\u88ab\u6b63\u786e\u5730\u8f6c\u6362\u4e3a-1\u5c0f\u65f6-1\u5206\u949f-1\u79d2\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u5904\u7406\u5927\u4e8e24\u5c0f\u65f6\u7684\u65f6\u95f4<\/h2>\n<\/p>\n<p><p>\u5f53\u79d2\u6570\u5927\u4e8e24\u5c0f\u65f6\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u5929\u6570\u3001\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u3002\u53ef\u4ee5\u4f7f\u7528datetime\u5e93\u7684timedelta\u7c7b\u6216\u624b\u52a8\u8ba1\u7b97\u6765\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def convert_large_seconds(seconds):<\/p>\n<p>    days = seconds \/\/ 86400<\/p>\n<p>    remaining_seconds = seconds % 86400<\/p>\n<p>    hours, remainder = divmod(remaining_seconds, 3600)<\/p>\n<p>    minutes, remaining_seconds = divmod(remainder, 60)<\/p>\n<p>    return days, hours, minutes, remaining_seconds<\/p>\n<p>seconds = 90061<\/p>\n<p>days, hours, minutes, remaining_seconds = convert_large_seconds(seconds)<\/p>\n<p>print(f&quot;{days}\u5929, {hours}\u5c0f\u65f6, {minutes}\u5206\u949f, {remaining_seconds}\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c90061\u79d2\u88ab\u8f6c\u6362\u4e3a1\u59291\u5c0f\u65f61\u5206\u949f1\u79d2\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5904\u7406\u4efb\u610f\u5927\u4e8e24\u5c0f\u65f6\u7684\u79d2\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u5904\u7406\u6beb\u79d2\u548c\u5fae\u79d2<\/h2>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u9ad8\u7cbe\u5ea6\u65f6\u95f4\u8ba1\u7b97\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5904\u7406\u6beb\u79d2\u548c\u5fae\u79d2\u3002\u53ef\u4ee5\u4f7f\u7528datetime\u5e93\u7684timedelta\u7c7b\u6765\u5904\u7406\u6beb\u79d2\u548c\u5fae\u79d2\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u5c0f\u65f6\u3001\u5206\u949f\u3001\u79d2\u3001\u6beb\u79d2\u548c\u5fae\u79d2\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import timedelta<\/p>\n<p>def convert_seconds_with_milliseconds(seconds):<\/p>\n<p>    delta = timedelta(seconds=seconds)<\/p>\n<p>    days = delta.days<\/p>\n<p>    hours = delta.seconds \/\/ 3600<\/p>\n<p>    minutes = (delta.seconds % 3600) \/\/ 60<\/p>\n<p>    remaining_seconds = delta.seconds % 60<\/p>\n<p>    milliseconds = delta.microseconds \/\/ 1000<\/p>\n<p>    microseconds = delta.microseconds % 1000<\/p>\n<p>    return days, hours, minutes, remaining_seconds, milliseconds, microseconds<\/p>\n<p>seconds = 90061.123456<\/p>\n<p>days, hours, minutes, remaining_seconds, milliseconds, microseconds = convert_seconds_with_milliseconds(seconds)<\/p>\n<p>print(f&quot;{days}\u5929, {hours}\u5c0f\u65f6, {minutes}\u5206\u949f, {remaining_seconds}\u79d2, {milliseconds}\u6beb\u79d2, {microseconds}\u5fae\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c90061.123456\u79d2\u88ab\u8f6c\u6362\u4e3a1\u59291\u5c0f\u65f61\u5206\u949f1\u79d2123\u6beb\u79d2456\u5fae\u79d2\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9ad8\u7cbe\u5ea6\u65f6\u95f4\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h2>\u516b\u3001\u5e94\u7528\u5b9e\u4f8b<\/h2>\n<\/p>\n<p><h3>\u5b9a\u65f6\u5668<\/h3>\n<\/p>\n<p><p>\u5b9a\u65f6\u5668\u53ef\u4ee5\u4f7f\u7528\u79d2\u6570\u8f6c\u6362\u4e3a\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u6765\u663e\u793a\u5269\u4f59\u65f6\u95f4\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u5012\u8ba1\u65f6\u5b9a\u65f6\u5668\u53ef\u4ee5\u663e\u793a\u201c00:05:30\u201d\u8868\u793a\u5269\u4f595\u5206\u949f30\u79d2\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def countdown_timer(seconds):<\/p>\n<p>    while seconds &gt; 0:<\/p>\n<p>        hours, minutes, remaining_seconds = convert_seconds_with_divmod(seconds)<\/p>\n<p>        print(f&quot;{hours:02}:{minutes:02}:{remaining_seconds:02}&quot;, end=&quot;\\r&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>        seconds -= 1<\/p>\n<p>    print(&quot;Time&#39;s up!&quot;)<\/p>\n<p>countdown_timer(330)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8bb0\u5f55\u65f6\u95f4\u95f4\u9694<\/h3>\n<\/p>\n<p><p>\u5728\u6027\u80fd\u5206\u6790\u6216\u65e5\u5fd7\u8bb0\u5f55\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u8bb0\u5f55\u65f6\u95f4\u95f4\u9694\u3002\u53ef\u4ee5\u4f7f\u7528\u79d2\u6570\u8f6c\u6362\u4e3a\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u6765\u663e\u793a\u65f6\u95f4\u95f4\u9694\u3002\u4f8b\u5982\uff0c\u8bb0\u5f55\u4e00\u4e2a\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def example_function():<\/p>\n<p>    time.sleep(2)<\/p>\n<p>start_time = time.time()<\/p>\n<p>example_function()<\/p>\n<p>end_time = time.time()<\/p>\n<p>elapsed_seconds = end_time - start_time<\/p>\n<p>hours, minutes, remaining_seconds = convert_seconds_with_divmod(elapsed_seconds)<\/p>\n<p>print(f&quot;\u51fd\u6570\u6267\u884c\u65f6\u95f4: {hours}\u5c0f\u65f6, {minutes}\u5206\u949f, {remaining_seconds:.2f}\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e5d\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u591a\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u79d2\u6570\u8f6c\u6362\u4e3a\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5904\u7406\u79d2\u6570\u8f6c\u6362\u3002\u6574\u9664\u548c\u53d6\u4f59\u8fd0\u7b97\u9002\u7528\u4e8e\u7b80\u5355\u9700\u6c42\uff0cdatetime\u5e93\u9002\u7528\u4e8e\u590d\u6742\u65f6\u95f4\u5904\u7406\uff0cdivmod\u51fd\u6570\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0chumanize\u5e93\u63d0\u4f9b\u4eba\u6027\u5316\u7684\u65f6\u95f4\u8868\u793a\uff0c\u9ad8\u7cbe\u5ea6\u65f6\u95f4\u5904\u7406\u9002\u7528\u4e8e\u6beb\u79d2\u548c\u5fae\u79d2\u7ea7\u522b\u7684\u8ba1\u7b97\u3002\u901a\u8fc7\u7ed3\u5408\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u5904\u7406\u5404\u79cd\u65f6\u95f4\u8f6c\u6362\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06\u79d2\u6570\u8f6c\u6362\u4e3a\u65f6\u5206\u79d2\u683c\u5f0f\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570\u548c\u8fd0\u7b97\u7b26\u6765\u5c06\u79d2\u6570\u8f6c\u6362\u4e3a\u65f6\u3001\u5206\u548c\u79d2\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u6574\u9664\u8fd0\u7b97\u7b26\uff08\/\/\uff09\u548c\u53d6\u4f59\u8fd0\u7b97\u7b26\uff08%\uff09\u6765\u5206\u522b\u8ba1\u7b97\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u3002\u5177\u4f53\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">total_seconds = 3661\nhours = total_seconds \/\/ 3600\nminutes = (total_seconds % 3600) \/\/ 60\nseconds = total_seconds % 60\nprint(f&quot;{hours}\u5c0f\u65f6 {minutes}\u5206\u949f {seconds}\u79d2&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa\u201c1\u5c0f\u65f6 1\u5206\u949f 1\u79d2\u201d\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u5904\u7406\u65f6\u95f4\u548c\u65e5\u671f\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u4e2a\u5e93\u6765\u5904\u7406\u65f6\u95f4\u548c\u65e5\u671f\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662f<code>datetime<\/code>\u6a21\u5757\u548c<code>time<\/code>\u6a21\u5757\u3002<code>datetime<\/code>\u6a21\u5757\u53ef\u4ee5\u8f7b\u677e\u5730\u5904\u7406\u65f6\u95f4\u7684\u8f6c\u6362\u3001\u683c\u5f0f\u5316\u7b49\u64cd\u4f5c\uff0c\u800c<code>time<\/code>\u6a21\u5757\u5219\u66f4\u9002\u5408\u4f4e\u7ea7\u65f6\u95f4\u5904\u7406\u3002\u4f7f\u7528\u8fd9\u4e9b\u5e93\u53ef\u4ee5\u7b80\u5316\u79d2\u6570\u5230\u65f6\u5206\u79d2\u7684\u8f6c\u6362\u8fc7\u7a0b\uff0c\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u5927\u89c4\u6a21\u65f6\u95f4\u6570\u636e\u65f6\uff0c\u5982\u4f55\u4f18\u5316\u6027\u80fd\uff1f<\/strong><br \/>\u5f53\u9700\u8981\u5904\u7406\u5927\u91cf\u7684\u65f6\u95f4\u6570\u636e\u65f6\uff0c\u4f18\u5316\u6027\u80fd\u53d8\u5f97\u5c24\u4e3a\u91cd\u8981\u3002\u53ef\u4ee5\u8003\u8651\u4f7f\u7528NumPy\u5e93\uff0c\u5b83\u652f\u6301\u9ad8\u6548\u7684\u6570\u7ec4\u8fd0\u7b97\uff0c\u80fd\u5728\u5904\u7406\u65f6\u95f4\u6570\u636e\u65f6\u663e\u8457\u63d0\u9ad8\u901f\u5ea6\u3002\u6b64\u5916\uff0c\u907f\u514d\u5728\u5faa\u73af\u4e2d\u91cd\u590d\u8ba1\u7b97\u53ef\u4ee5\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u5f00\u9500\uff0c\u5c3d\u91cf\u5728\u5faa\u73af\u5916\u9884\u5148\u8ba1\u7b97\u5e38\u91cf\u503c\u3002\u4f7f\u7528\u5411\u91cf\u5316\u64cd\u4f5c\u4e5f\u662f\u4e00\u79cd\u6709\u6548\u7684\u4f18\u5316\u7b56\u7565\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u901a\u8fc7\u79d2\u6570\u6c42\u65f6\u5206\u79d2\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u6574\u9664\u548c\u53d6\u4f59\u8fd0\u7b97\u3001\u4f7f\u7528\u5185\u7f6e\u5e93datetime\u3001\u4f7f\u7528di [&hellip;]","protected":false},"author":3,"featured_media":1096773,"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\/1096768"}],"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=1096768"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1096768\/revisions"}],"predecessor-version":[{"id":1096775,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1096768\/revisions\/1096775"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1096773"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1096768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1096768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1096768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}