{"id":974580,"date":"2024-12-27T06:06:19","date_gmt":"2024-12-26T22:06:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/974580.html"},"modified":"2024-12-27T06:06:22","modified_gmt":"2024-12-26T22:06:22","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e6%97%a5%e5%8e%86%e8%be%93%e5%87%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/974580.html","title":{"rendered":"python\u5982\u4f55\u5b9e\u73b0\u65e5\u5386\u8f93\u51fa"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200226\/bf7a5203-a6e1-4948-afe2-995d42f6e28a.webp\" alt=\"python\u5982\u4f55\u5b9e\u73b0\u65e5\u5386\u8f93\u51fa\" \/><\/p>\n<p><p> <strong>Python\u5b9e\u73b0\u65e5\u5386\u8f93\u51fa\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684<code>calendar<\/code>\u6a21\u5757\u3001\u5229\u7528<code>datetime<\/code>\u6a21\u5757\u683c\u5f0f\u5316\u8f93\u51fa\u3001\u7ed3\u5408<code>matplotlib<\/code>\u7b49\u5e93\u5b9e\u73b0\u81ea\u5b9a\u4e49\u65e5\u5386\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528\u5185\u7f6e\u7684<code>calendar<\/code>\u6a21\u5757\u662f\u6700\u76f4\u63a5\u548c\u7b80\u4fbf\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65e5\u5386\u76f8\u5173\u529f\u80fd\uff0c\u5305\u62ec\u8f93\u51fa\u6587\u672c\u65e5\u5386\u3001HTML\u65e5\u5386\u4ee5\u53ca\u8ba1\u7b97\u65e5\u671f\u95f4\u9694\u7b49\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u7684\u4e0d\u540c\u529f\u80fd\u548c\u5e93\u6765\u5b9e\u73b0\u65e5\u5386\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>calendar<\/code>\u6a21\u5757\u8f93\u51fa\u6587\u672c\u65e5\u5386<\/p>\n<\/p>\n<p><p>Python\u7684<code>calendar<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684\u529f\u80fd\u6765\u5904\u7406\u5e74\u5386\u548c\u6708\u5386\u3002\u53ef\u4ee5\u8f7b\u677e\u8f93\u51fa\u67d0\u4e2a\u6708\u4efd\u6216\u67d0\u5e74\u7684\u65e5\u5386\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528<code>calendar.month()<\/code>\u8f93\u51fa\u6708\u5386<\/h3>\n<\/p>\n<p><p><code>calendar.month(year, month)<\/code>\u51fd\u6570\u53ef\u4ee5\u8f93\u51fa\u6307\u5b9a\u5e74\u4efd\u548c\u6708\u4efd\u7684\u6587\u672c\u65e5\u5386\u3002\u8be5\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6307\u5b9a\u6708\u4efd\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import calendar<\/p>\n<h2><strong>\u8f93\u51fa2023\u5e7410\u6708\u7684\u65e5\u5386<\/strong><\/h2>\n<p>year = 2023<\/p>\n<p>month = 10<\/p>\n<p>print(calendar.month(year, month))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528<code>calendar.prmonth()<\/code>\u76f4\u63a5\u6253\u5370\u6708\u5386<\/h3>\n<\/p>\n<p><p><code>calendar.prmonth(year, month)<\/code>\u51fd\u6570\u76f4\u63a5\u5728\u63a7\u5236\u53f0\u6253\u5370\u51fa\u6307\u5b9a\u6708\u4efd\u7684\u65e5\u5386\uff0c\u800c\u4e0d\u9700\u8981\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import calendar<\/p>\n<h2><strong>\u76f4\u63a5\u6253\u53702023\u5e7410\u6708\u7684\u65e5\u5386<\/strong><\/h2>\n<p>calendar.prmonth(2023, 10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u4f7f\u7528<code>calendar.calendar()<\/code>\u8f93\u51fa\u5e74\u5386<\/h3>\n<\/p>\n<p><p><code>calendar.calendar(year)<\/code>\u51fd\u6570\u8fd4\u56de\u6307\u5b9a\u5e74\u4efd\u7684\u6574\u4e2a\u5e74\u5386\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import calendar<\/p>\n<h2><strong>\u8f93\u51fa2023\u5e74\u7684\u5e74\u5386<\/strong><\/h2>\n<p>year = 2023<\/p>\n<p>print(calendar.calendar(year))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4. \u4f7f\u7528<code>calendar.prcal()<\/code>\u76f4\u63a5\u6253\u5370\u5e74\u5386<\/h3>\n<\/p>\n<p><p><code>calendar.prcal(year)<\/code>\u51fd\u6570\u76f4\u63a5\u5728\u63a7\u5236\u53f0\u6253\u5370\u51fa\u6307\u5b9a\u5e74\u4efd\u7684\u5e74\u5386\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import calendar<\/p>\n<h2><strong>\u76f4\u63a5\u6253\u53702023\u5e74\u7684\u5e74\u5386<\/strong><\/h2>\n<p>calendar.prcal(2023)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u683c\u5f0f\u5316\u8f93\u51fa<\/p>\n<\/p>\n<p><p>\u867d\u7136<code>datetime<\/code>\u6a21\u5757\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u4f46\u7ed3\u5408\u5176\u4ed6\u529f\u80fd\u53ef\u4ee5\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u65e5\u5386\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>1. \u83b7\u53d6\u5f53\u524d\u65e5\u671f\u5e76\u683c\u5f0f\u5316<\/h3>\n<\/p>\n<p><p><code>datetime<\/code>\u6a21\u5757\u4e2d\u7684<code>datetime.now()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u7136\u540e\u53ef\u4ee5\u4f7f\u7528<code>strftime()<\/code>\u65b9\u6cd5\u8fdb\u884c\u683c\u5f0f\u5316\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65e5\u671f<\/strong><\/h2>\n<p>now = datetime.now()<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u8f93\u51fa\u65e5\u671f<\/strong><\/h2>\n<p>formatted_date = now.strftime(&quot;%Y-%m-%d&quot;)<\/p>\n<p>print(f&quot;Today&#39;s date is: {formatted_date}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u8ba1\u7b97\u65e5\u671f\u95f4\u9694<\/h3>\n<\/p>\n<p><p><code>datetime<\/code>\u6a21\u5757\u8fd8\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u95f4\u9694\uff0c\u8fd9\u5728\u65e5\u5386\u5e94\u7528\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u5b9a\u4e49\u4e24\u4e2a\u65e5\u671f<\/strong><\/h2>\n<p>date1 = datetime(2023, 10, 1)<\/p>\n<p>date2 = datetime(2023, 10, 31)<\/p>\n<h2><strong>\u8ba1\u7b97\u65e5\u671f\u95f4\u9694<\/strong><\/h2>\n<p>delta = date2 - date1<\/p>\n<p>print(f&quot;The number of days between {date1.strftime(&#39;%Y-%m-%d&#39;)} and {date2.strftime(&#39;%Y-%m-%d&#39;)} is {delta.days} days.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u7ed3\u5408<code>matplotlib<\/code>\u7b49\u5e93\u5b9e\u73b0\u81ea\u5b9a\u4e49\u65e5\u5386<\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u66f4\u590d\u6742\u548c\u81ea\u5b9a\u4e49\u7684\u65e5\u5386\u8f93\u51fa\uff0c\u6bd4\u5982\u5e26\u6709\u56fe\u5f62\u5316\u754c\u9762\u7684\u65e5\u5386\uff0c\u53ef\u4ee5\u7ed3\u5408<code>matplotlib<\/code>\u7b49\u53ef\u89c6\u5316\u5e93\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528<code>matplotlib<\/code>\u7ed8\u5236\u7b80\u5355\u65e5\u5386<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>matplotlib<\/code>\u7ed8\u5236\u4e00\u4e2a\u7b80\u5355\u7684\u65e5\u5386\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import calendar<\/p>\n<p>year = 2023<\/p>\n<p>month = 10<\/p>\n<h2><strong>\u83b7\u53d6\u6708\u5386\u7684\u77e9\u9635\u5f62\u5f0f<\/strong><\/h2>\n<p>month_days = calendar.monthcalendar(year, month)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u56fe\u5f62\u548c\u5b50\u56fe<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u7ed8\u5236\u65e5\u5386<\/strong><\/h2>\n<p>ax.set_axis_off()<\/p>\n<p>table = ax.table(cellText=month_days, colLabels=[&quot;Mon&quot;, &quot;Tue&quot;, &quot;Wed&quot;, &quot;Thu&quot;, &quot;Fri&quot;, &quot;Sat&quot;, &quot;Sun&quot;], loc=&#39;center&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u8868\u683c\u7684\u5b57\u4f53\u5927\u5c0f<\/strong><\/h2>\n<p>table.auto_set_font_size(False)<\/p>\n<p>table.set_fontsize(14)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.title(f&#39;{calendar.month_name[month]} {year}&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u81ea\u5b9a\u4e49\u65e5\u5386\u683c\u5f0f<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7<code>matplotlib<\/code>\u548c\u5176\u4ed6\u5e93\uff0c\u60a8\u53ef\u4ee5\u8fdb\u4e00\u6b65\u81ea\u5b9a\u4e49\u65e5\u5386\u7684\u6837\u5f0f\uff0c\u5305\u62ec\u8bbe\u7f6e\u4e0d\u540c\u7684\u989c\u8272\u3001\u5b57\u4f53\u548c\u5e03\u5c40\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import calendar<\/p>\n<p>from datetime import datetime<\/p>\n<p>year = 2023<\/p>\n<p>month = 10<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65e5\u671f<\/strong><\/h2>\n<p>today = datetime.now().day<\/p>\n<h2><strong>\u83b7\u53d6\u6708\u5386\u7684\u77e9\u9635\u5f62\u5f0f<\/strong><\/h2>\n<p>month_days = calendar.monthcalendar(year, month)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u56fe\u5f62\u548c\u5b50\u56fe<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u7ed8\u5236\u65e5\u5386<\/strong><\/h2>\n<p>ax.set_axis_off()<\/p>\n<p>table = ax.table(cellText=month_days, colLabels=[&quot;Mon&quot;, &quot;Tue&quot;, &quot;Wed&quot;, &quot;Thu&quot;, &quot;Fri&quot;, &quot;Sat&quot;, &quot;Sun&quot;], loc=&#39;center&#39;)<\/p>\n<h2><strong>\u81ea\u5b9a\u4e49\u8868\u683c\u6837\u5f0f<\/strong><\/h2>\n<p>for i, row in enumerate(month_days):<\/p>\n<p>    for j, day in enumerate(row):<\/p>\n<p>        if day == today:<\/p>\n<p>            # \u5f53\u524d\u65e5\u671f\u80cc\u666f\u8272<\/p>\n<p>            cell = table.get_celld()[(i+1, j)]<\/p>\n<p>            cell.set_facecolor(&quot;#FFDD44&quot;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u8868\u683c\u7684\u5b57\u4f53\u5927\u5c0f<\/strong><\/h2>\n<p>table.auto_set_font_size(False)<\/p>\n<p>table.set_fontsize(14)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.title(f&#39;{calendar.month_name[month]} {year}&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u5404\u79cd\u5f62\u5f0f\u7684\u65e5\u5386\u8f93\u51fa\uff0c\u4ece\u7b80\u5355\u7684\u6587\u672c\u65e5\u5386\u5230\u590d\u6742\u7684\u56fe\u5f62\u5316\u65e5\u5386\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u751f\u6210\u4e00\u4e2a\u7b80\u5355\u7684\u65e5\u5386\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u751f\u6210\u7b80\u5355\u7684\u65e5\u5386\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>calendar<\/code>\u6a21\u5757\u3002\u9996\u5148\uff0c\u5bfc\u5165\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>calendar.month(year, month)<\/code>\u65b9\u6cd5\u6765\u6253\u5370\u6307\u5b9a\u5e74\u4efd\u548c\u6708\u4efd\u7684\u65e5\u5386\u3002\u4f8b\u5982\uff0c<code>print(calendar.month(2023, 10))<\/code>\u5c06\u8f93\u51fa2023\u5e7410\u6708\u7684\u65e5\u5386\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u8282\u5047\u65e5\u7684\u65e5\u5386\u6709\u4ec0\u4e48\u65b9\u6cd5\uff1f<\/strong><br \/>\u53ef\u4ee5\u5229\u7528<code>calendar<\/code>\u6a21\u5757\u7ed3\u5408\u5176\u4ed6\u5e93\u6765\u521b\u5efa\u5305\u542b\u8282\u5047\u65e5\u7684\u65e5\u5386\u3002\u9996\u5148\u751f\u6210\u57fa\u672c\u65e5\u5386\uff0c\u7136\u540e\u4f7f\u7528Python\u7684<code>datetime<\/code>\u6a21\u5757\u6765\u6807\u8bb0\u7279\u5b9a\u65e5\u671f\u4f5c\u4e3a\u8282\u5047\u65e5\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u5b58\u50a8\u8282\u5047\u65e5\u7684\u65e5\u671f\u548c\u540d\u79f0\uff0c\u7136\u540e\u5728\u8f93\u51fa\u65e5\u5386\u65f6\uff0c\u5c06\u8fd9\u4e9b\u65e5\u671f\u8fdb\u884c\u9ad8\u4eae\u663e\u793a\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u81ea\u5b9a\u4e49\u65e5\u5386\u7684\u683c\u5f0f\uff1f<\/strong><br \/>\u4e3a\u4e86\u81ea\u5b9a\u4e49\u65e5\u5386\u7684\u8f93\u51fa\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>calendar<\/code>\u6a21\u5757\u7684<code>TextCalendar<\/code>\u7c7b\u3002\u901a\u8fc7\u91cd\u5199<code>formatmonth()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6539\u53d8\u65e5\u5386\u7684\u5e03\u5c40\u548c\u6837\u5f0f\u3002\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u529f\u80fd\uff0c\u8c03\u6574\u8f93\u51fa\u7684\u5bf9\u9f50\u548c\u6392\u7248\uff0c\u4ee5\u6ee1\u8db3\u7279\u5b9a\u7684\u89c6\u89c9\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5b9e\u73b0\u65e5\u5386\u8f93\u51fa\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684calendar\u6a21\u5757\u3001\u5229\u7528datetime\u6a21\u5757\u683c\u5f0f\u5316\u8f93\u51fa\u3001\u7ed3\u5408mat [&hellip;]","protected":false},"author":3,"featured_media":974590,"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\/974580"}],"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=974580"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/974580\/revisions"}],"predecessor-version":[{"id":974594,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/974580\/revisions\/974594"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/974590"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=974580"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=974580"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=974580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}