{"id":932601,"date":"2024-12-26T18:01:19","date_gmt":"2024-12-26T10:01:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/932601.html"},"modified":"2024-12-26T18:01:22","modified_gmt":"2024-12-26T10:01:22","slug":"%e5%a6%82%e4%bd%95%e6%89%93%e5%8d%b0%e6%97%a5%e5%8e%86-python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/932601.html","title":{"rendered":"\u5982\u4f55\u6253\u5370\u65e5\u5386 Python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070412\/c3d5e8d0-bdfc-402e-a980-78026c3f573d.webp\" alt=\"\u5982\u4f55\u6253\u5370\u65e5\u5386 Python\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u6253\u5370\u65e5\u5386\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684<code>calendar<\/code>\u6a21\u5757\u3001\u5229\u7528\u5faa\u73af\u751f\u6210\u81ea\u5b9a\u4e49\u65e5\u5386\u3001\u6216\u8005\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u3002<\/strong>\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u5229\u7528Python\u7684<code>calendar<\/code>\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u751f\u6210\u548c\u64cd\u4f5c\u65e5\u5386\u3002\u901a\u8fc7<code>calendar<\/code>\u6a21\u5757\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u6253\u5370\u51fa\u6307\u5b9a\u6708\u4efd\u7684\u65e5\u5386\u3001\u8ba1\u7b97\u7279\u5b9a\u65e5\u671f\u7684\u76f8\u5173\u4fe1\u606f\u4ee5\u53ca\u8fdb\u884c\u65e5\u671f\u7684\u5404\u79cd\u64cd\u4f5c\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u5c55\u793a\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>calendar<\/code>\u6a21\u5757\u6253\u5370\u65e5\u5386<\/p>\n<\/p>\n<p><p>Python\u7684<code>calendar<\/code>\u6a21\u5757\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u80fd\u591f\u65b9\u4fbf\u5730\u751f\u6210\u548c\u64cd\u4f5c\u65e5\u5386\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u529f\u80fd\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u6253\u5370\u6307\u5b9a\u6708\u4efd\u7684\u65e5\u5386<\/strong><\/li>\n<\/ol>\n<p><p>\u8981\u6253\u5370\u67d0\u5e74\u67d0\u6708\u7684\u65e5\u5386\uff0c\u53ef\u4ee5\u4f7f\u7528<code>calendar.month()<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u8be5\u6708\u65e5\u5386\u7684\u591a\u884c\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import calendar<\/p>\n<h2><strong>\u751f\u62102023\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<ol start=\"2\">\n<li><strong>\u6253\u5370\u6307\u5b9a\u5e74\u4efd\u7684\u65e5\u5386<\/strong><\/li>\n<\/ol>\n<p><p>\u5982\u679c\u9700\u8981\u6253\u5370\u6574\u4e2a\u5e74\u4efd\u7684\u65e5\u5386\uff0c\u53ef\u4ee5\u4f7f\u7528<code>calendar.calendar()<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5168\u5e74\u65e5\u5386\u7684\u591a\u884c\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import calendar<\/p>\n<h2><strong>\u751f\u62102023\u5e74\u7684\u65e5\u5386<\/strong><\/h2>\n<p>year = 2023<\/p>\n<p>print(calendar.calendar(year))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><code>calendar<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86<code>TextCalendar<\/code>\u548c<code>HTMLCalendar<\/code>\u7c7b\uff0c\u5206\u522b\u7528\u4e8e\u751f\u6210\u6587\u672c\u683c\u5f0f\u548cHTML\u683c\u5f0f\u7684\u65e5\u5386\u3002\u901a\u8fc7\u521b\u5efa<code>TextCalendar<\/code>\u5bf9\u8c61\uff0c\u53ef\u4ee5\u8c03\u7528<code>formatmonth()<\/code>\u65b9\u6cd5\u6765\u83b7\u5f97\u6307\u5b9a\u6708\u4efd\u7684\u65e5\u5386\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import calendar<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6587\u672c\u65e5\u5386<\/strong><\/h2>\n<p>cal = calendar.TextCalendar()<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u5e76\u6253\u53702023\u5e7410\u6708\u7684\u65e5\u5386<\/strong><\/h2>\n<p>print(cal.formatmonth(2023, 10))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u751f\u6210\u81ea\u5b9a\u4e49\u65e5\u5386<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u751f\u6210\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684\u65e5\u5386\uff0c\u4ee5\u6ee1\u8db3\u7279\u5b9a\u7684\u9700\u6c42\u3002\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u548c\u65e5\u671f\u8fd0\u7b97\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u751f\u6210\u4e00\u4e2a\u7b80\u5355\u7684\u6708\u5386<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u624b\u52a8\u4f7f\u7528\u5faa\u73af\u548c\u65e5\u671f\u8fd0\u7b97\u6765\u751f\u6210\u4e00\u4e2a\u7b80\u5355\u7684\u6708\u5386\u3002\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u4f60\u5b8c\u5168\u81ea\u5b9a\u4e49\u65e5\u5386\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import datetime<\/p>\n<p>def generate_calendar(year, month):<\/p>\n<p>    # \u83b7\u53d6\u6307\u5b9a\u6708\u4efd\u7684\u7b2c\u4e00\u5929\u548c\u6700\u540e\u4e00\u5929<\/p>\n<p>    first_day = datetime.date(year, month, 1)<\/p>\n<p>    last_day = datetime.date(year, month + 1, 1) - datetime.timedelta(days=1)<\/p>\n<p>    # \u6253\u5370\u6708\u548c\u5e74\u4efd\u7684\u6807\u9898<\/p>\n<p>    print(f&quot;{first_day.strftime(&#39;%B %Y&#39;)}&quot;)<\/p>\n<p>    print(&quot;Mo Tu We Th Fr Sa Su&quot;)<\/p>\n<p>    # \u627e\u5230\u7b2c\u4e00\u5929\u662f\u661f\u671f\u51e0<\/p>\n<p>    start_weekday = first_day.weekday()<\/p>\n<p>    # \u6253\u5370\u65e5\u5386\u7684\u6bcf\u4e00\u884c<\/p>\n<p>    day = 1<\/p>\n<p>    for week in range(6):  # 6\u5468\u8db3\u591f\u8986\u76d6\u6240\u6709\u6708\u4efd<\/p>\n<p>        for weekday in range(7):<\/p>\n<p>            if (week == 0 and weekday &lt; start_weekday) or day &gt; last_day.day:<\/p>\n<p>                # \u6253\u5370\u7a7a\u767d<\/p>\n<p>                print(&quot;   &quot;, end=&quot;&quot;)<\/p>\n<p>            else:<\/p>\n<p>                # \u6253\u5370\u65e5\u671f<\/p>\n<p>                print(f&quot;{day:2} &quot;, end=&quot;&quot;)<\/p>\n<p>                day += 1<\/p>\n<p>        print()<\/p>\n<h2><strong>\u751f\u62102023\u5e7410\u6708\u7684\u65e5\u5386<\/strong><\/h2>\n<p>generate_calendar(2023, 10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u81ea\u5b9a\u4e49\u5468\u8d77\u59cb\u65e5<\/strong><\/li>\n<\/ol>\n<p><p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>calendar<\/code>\u6a21\u5757\u7684\u5468\u8d77\u59cb\u65e5\u662f\u661f\u671f\u4e00\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>setfirstweekday()<\/code>\u6765\u66f4\u6539\u8fd9\u4e00\u70b9\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u5468\u8d77\u59cb\u65e5\u8bbe\u7f6e\u4e3a\u661f\u671f\u65e5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import calendar<\/p>\n<h2><strong>\u8bbe\u7f6e\u5468\u8d77\u59cb\u65e5\u4e3a\u661f\u671f\u65e5<\/strong><\/h2>\n<p>calendar.setfirstweekday(calendar.SUNDAY)<\/p>\n<h2><strong>\u6253\u53702023\u5e7410\u6708\u7684\u65e5\u5386<\/strong><\/h2>\n<p>print(calendar.month(2023, 10))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5229\u7528\u7b2c\u4e09\u65b9\u5e93\u751f\u6210\u590d\u6742\u65e5\u5386<\/p>\n<\/p>\n<p><p>\u867d\u7136<code>calendar<\/code>\u6a21\u5757\u8db3\u591f\u5f3a\u5927\uff0c\u4f46\u6709\u65f6\u5019\u4f60\u53ef\u80fd\u9700\u8981\u66f4\u590d\u6742\u7684\u529f\u80fd\uff0c\u6bd4\u5982\u4e8b\u4ef6\u63d0\u9192\u3001\u65e5\u7a0b\u5b89\u6392\u7b49\u3002\u8fd9\u65f6\uff0c\u53ef\u4ee5\u501f\u52a9\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982<code>dateutil<\/code>\u6216<code>pandas<\/code>\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528<code>dateutil<\/code>\u8fdb\u884c\u65e5\u671f\u8ba1\u7b97<\/strong><\/li>\n<\/ol>\n<p><p><code>dateutil<\/code>\u5e93\u6269\u5c55\u4e86\u6807\u51c6\u5e93<code>datetime<\/code>\u6a21\u5757\u7684\u529f\u80fd\uff0c\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u65e5\u671f\u89e3\u6790\u548c\u8ba1\u7b97\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install python-dateutil<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">from dateutil.relativedelta import relativedelta<\/p>\n<p>from datetime import datetime<\/p>\n<h2><strong>\u8ba1\u7b97\u4ece\u4eca\u5929\u5f00\u59cb\u7684\u4e0b\u4e00\u4e2a\u6708<\/strong><\/h2>\n<p>today = datetime.now()<\/p>\n<p>next_month = today + relativedelta(months=1)<\/p>\n<p>print(f&quot;Next month: {next_month.strftime(&#39;%B %Y&#39;)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528<code>pandas<\/code>\u8fdb\u884c\u65e5\u671f\u5206\u6790<\/strong><\/li>\n<\/ol>\n<p><p><code>pandas<\/code>\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5de5\u5177\uff0c\u4e5f\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65e5\u671f\u5904\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u65e5\u671f\u8303\u56f4\u7684DataFrame<\/strong><\/h2>\n<p>date_range = pd.date_range(start=&#39;2023-10-01&#39;, end=&#39;2023-10-31&#39;, freq=&#39;D&#39;)<\/p>\n<p>df = pd.DataFrame(date_range, columns=[&#39;Date&#39;])<\/p>\n<h2><strong>\u6253\u5370DataFrame<\/strong><\/h2>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u603b\u7ed3\uff1a<\/strong>\u901a\u8fc7Python\u751f\u6210\u548c\u6253\u5370\u65e5\u5386\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u4f7f\u7528\u5185\u7f6e\u7684<code>calendar<\/code>\u6a21\u5757\u662f\u6700\u7b80\u5355\u548c\u76f4\u63a5\u7684\u65b9\u5f0f\uff0c\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u51fd\u6570\u6765\u683c\u5f0f\u5316\u548c\u64cd\u4f5c\u65e5\u671f\u3002\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u4ee3\u7801\u6216\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u6765\u5b9e\u73b0\u3002\u65e0\u8bba\u662f\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u7075\u6d3b\u5e94\u7528\uff0c\u4e3a\u65e5\u5e38\u5de5\u4f5c\u548c\u9879\u76ee\u5f00\u53d1\u63d0\u4f9b\u4fbf\u5229\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u6253\u5370\u7279\u5b9a\u6708\u4efd\u7684\u65e5\u5386\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>calendar<\/code>\u6a21\u5757\u6765\u6253\u5370\u7279\u5b9a\u6708\u4efd\u7684\u65e5\u5386\u3002\u53ea\u9700\u5bfc\u5165\u8be5\u6a21\u5757\u5e76\u4f7f\u7528<code>month<\/code>\u51fd\u6570\uff0c\u4f20\u5165\u5e74\u4efd\u548c\u6708\u4efd\u5373\u53ef\u3002\u4f8b\u5982\uff0c<code>calendar.month(2023, 10)<\/code>\u5c06\u6253\u53702023\u5e7410\u6708\u7684\u65e5\u5386\u3002\u8fd9\u4e2a\u65b9\u6cd5\u8fd8\u652f\u6301\u9009\u62e9\u8f93\u51fa\u7684\u683c\u5f0f\uff0c\u65b9\u4fbf\u7528\u6237\u6839\u636e\u9700\u6c42\u8fdb\u884c\u8c03\u6574\u3002<\/p>\n<p><strong>\u6211\u9700\u8981\u5b89\u88c5\u989d\u5916\u7684\u5e93\u6765\u6253\u5370\u65e5\u5386\u5417\uff1f<\/strong><br \/>\u4e0d\u9700\u8981\uff0cPython\u7684<code>calendar<\/code>\u6a21\u5757\u662f\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u610f\u5473\u7740\u5b83\u5df2\u7ecf\u5305\u542b\u5728Python\u7684\u5b89\u88c5\u4e2d\u3002\u56e0\u6b64\uff0c\u65e0\u9700\u5b89\u88c5\u4efb\u4f55\u989d\u5916\u7684\u5e93\uff0c\u5c31\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u6b64\u6a21\u5757\u6765\u6253\u5370\u65e5\u5386\u3002<\/p>\n<p><strong>\u5982\u4f55\u81ea\u5b9a\u4e49\u6253\u5370\u7684\u65e5\u5386\u683c\u5f0f\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7<code>calendar<\/code>\u6a21\u5757\u63d0\u4f9b\u7684<code>TextCalendar<\/code>\u7c7b\u6765\u5b9e\u73b0\u81ea\u5b9a\u4e49\u683c\u5f0f\u6253\u5370\u3002\u7528\u6237\u53ef\u4ee5\u8bbe\u7f6e\u5468\u7684\u8d77\u59cb\u65e5\uff08\u5982\u5468\u4e00\u6216\u5468\u65e5\uff09\uff0c\u5e76\u901a\u8fc7<code>formatmonth<\/code>\u65b9\u6cd5\u8f93\u51fa\u7279\u5b9a\u6708\u4efd\u7684\u65e5\u5386\u3002\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u6765\u8c03\u6574\u65e5\u5386\u7684\u5916\u89c2\u548c\u683c\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u6253\u5370\u65e5\u5386\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684calendar\u6a21\u5757\u3001\u5229\u7528\u5faa\u73af\u751f\u6210\u81ea\u5b9a\u4e49\u65e5\u5386\u3001\u6216\u8005\u4f7f\u7528\u7b2c [&hellip;]","protected":false},"author":3,"featured_media":932606,"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\/932601"}],"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=932601"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/932601\/revisions"}],"predecessor-version":[{"id":932608,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/932601\/revisions\/932608"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/932606"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=932601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=932601"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=932601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}