{"id":1109447,"date":"2025-01-08T17:10:14","date_gmt":"2025-01-08T09:10:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1109447.html"},"modified":"2025-01-08T17:10:17","modified_gmt":"2025-01-08T09:10:17","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a9%e5%af%86%e7%a0%81%e5%8c%85%e5%90%ab%e4%b8%8b%e5%88%92%e7%ba%bf","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1109447.html","title":{"rendered":"python\u5982\u4f55\u8ba9\u5bc6\u7801\u5305\u542b\u4e0b\u5212\u7ebf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072724\/67fb1576-2a4d-4786-bbc9-bfddf9212d68.webp\" alt=\"python\u5982\u4f55\u8ba9\u5bc6\u7801\u5305\u542b\u4e0b\u5212\u7ebf\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u751f\u6210\u5305\u542b\u4e0b\u5212\u7ebf\u7684\u5bc6\u7801<\/strong>\uff0c\u53ef\u4ee5\u4f7f\u7528\u968f\u673a\u5b57\u7b26\u751f\u6210\u5668\u6765\u751f\u6210\u4e00\u4e2a\u5305\u542b\u6307\u5b9a\u5b57\u7b26\u96c6\uff08\u5305\u62ec\u4e0b\u5212\u7ebf\uff09\u7684\u5bc6\u7801\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>random<\/code>\u5e93\u6765\u968f\u673a\u9009\u62e9\u5b57\u7b26\u5e76\u751f\u6210\u5bc6\u7801\u3002\u4e0b\u9762\u662f\u8be6\u7ec6\u7684\u89e3\u91ca\u548c\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u548c\u6a21\u5757<\/p>\n<p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5bfc\u5165Python\u7684<code>random<\/code>\u5e93\u4e2d\u7684<code>choice<\/code>\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u4ece\u7ed9\u5b9a\u7684\u5b57\u7b26\u96c6\u4e2d\u968f\u673a\u9009\u62e9\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u5b9a\u4e49\u5b57\u7b26\u96c6<\/p>\n<p>\u4f60\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u6240\u6709\u53ef\u80fd\u5b57\u7b26\u7684\u5b57\u7b26\u96c6\u3002\u8fd9\u4e2a\u5b57\u7b26\u96c6\u5e94\u8be5\u5305\u62ec\u5c0f\u5199\u5b57\u6bcd\u3001\u5927\u5199\u5b57\u6bcd\u3001\u6570\u5b57\u4ee5\u53ca\u4e0b\u5212\u7ebf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">characters = &quot;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u6838\u5fc3\u89c2\u70b9\uff1a\u4f7f\u7528\u5b57\u7b26\u96c6\u751f\u6210\u968f\u673a\u5b57\u7b26\u3001\u786e\u4fdd\u4e0b\u5212\u7ebf\u5305\u542b\u5728\u5b57\u7b26\u96c6\u4e2d\u3001\u4f7f\u7528<code>random.choice<\/code>\u4ece\u5b57\u7b26\u96c6\u4e2d\u9009\u62e9\u5b57\u7b26<\/strong>\u3002\u4e0b\u9762\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u5b57\u7b26\u96c6\u751f\u6210\u968f\u673a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5b57\u7b26\u96c6\u751f\u6210\u968f\u673a\u5b57\u7b26\u662f\u751f\u6210\u5bc6\u7801\u7684\u57fa\u7840\u3002\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u6240\u6709\u53ef\u80fd\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u53ef\u4ee5\u4ece\u4e2d\u968f\u673a\u9009\u62e9\u5b57\u7b26\u6765\u6784\u6210\u5bc6\u7801\u3002\u786e\u4fdd\u5b57\u7b26\u96c6\u5305\u542b\u4e0b\u5212\u7ebf\u662f\u5173\u952e\uff0c\u8fd9\u6837\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u751f\u6210\u7684\u5bc6\u7801\u53ef\u80fd\u5305\u542b\u4e0b\u5212\u7ebf\u3002\u4f7f\u7528<code>random.choice<\/code>\u51fd\u6570\u4ece\u5b57\u7b26\u96c6\u4e2d\u9009\u62e9\u5b57\u7b26\uff0c\u7136\u540e\u5c06\u8fd9\u4e9b\u5b57\u7b26\u7ec4\u5408\u6210\u4e00\u4e2a\u5bc6\u7801\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u751f\u6210\u5bc6\u7801\u7684\u51fd\u6570<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u4f60\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u751f\u6210\u6307\u5b9a\u957f\u5ea6\u7684\u5bc6\u7801\u3002\u8fd9\u4e2a\u51fd\u6570\u4f1a\u4ece\u5b57\u7b26\u96c6\u4e2d\u968f\u673a\u9009\u62e9\u5b57\u7b26\u5e76\u7ec4\u5408\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def generate_password(length):<\/p>\n<p>    password = &#39;&#39;.join(random.choice(characters) for _ in range(length))<\/p>\n<p>    return password<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u8c03\u7528\u51fd\u6570\u5e76\u751f\u6210\u5bc6\u7801<\/p>\n<p>\u6700\u540e\uff0c\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u5e76\u751f\u6210\u4e00\u4e2a\u6307\u5b9a\u957f\u5ea6\u7684\u5bc6\u7801\u3002\u4f8b\u5982\uff0c\u8981\u751f\u6210\u4e00\u4e2a12\u4f4d\u7684\u5bc6\u7801\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">password = generate_password(12)<\/p>\n<p>print(password)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u786e\u4fdd\u5bc6\u7801\u5305\u542b\u4e0b\u5212\u7ebf<\/p>\n<p>\u4e3a\u4e86\u786e\u4fdd\u751f\u6210\u7684\u5bc6\u7801\u4e2d\u4e00\u5b9a\u5305\u542b\u4e0b\u5212\u7ebf\uff0c\u4f60\u53ef\u4ee5\u5728\u751f\u6210\u5bc6\u7801\u540e\u68c0\u67e5\u662f\u5426\u5305\u542b\u4e0b\u5212\u7ebf\u3002\u5982\u679c\u4e0d\u5305\u542b\uff0c\u53ef\u4ee5\u91cd\u65b0\u751f\u6210\u76f4\u5230\u5305\u542b\u4e3a\u6b62\uff0c\u6216\u8005\u5148\u52a0\u5165\u4e00\u4e2a\u4e0b\u5212\u7ebf\u518d\u968f\u673a\u751f\u6210\u5269\u4f59\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def generate_password_with_underscore(length):<\/p>\n<p>    if length &lt; 1:<\/p>\n<p>        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;Password length must be at least 1&quot;)<\/p>\n<p>    password = &#39;&#39;.join(random.choice(characters) for _ in range(length - 1)) + &#39;_&#39;<\/p>\n<p>    password = list(password)<\/p>\n<p>    random.shuffle(password)<\/p>\n<p>    return &#39;&#39;.join(password)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u793a\u4f8b\u4ee3\u7801\u5c55\u793a<\/p>\n<p>\u4e0b\u9762\u662f\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\u5c55\u793a\uff0c\u5305\u62ec\u786e\u4fdd\u5bc6\u7801\u4e2d\u5305\u542b\u4e0b\u5212\u7ebf\u7684\u903b\u8f91\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u5b9a\u4e49\u5b57\u7b26\u96c6\uff0c\u5305\u542b\u5c0f\u5199\u5b57\u6bcd\u3001\u5927\u5199\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf<\/strong><\/h2>\n<p>characters = &quot;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_&quot;<\/p>\n<h2><strong>\u751f\u6210\u6307\u5b9a\u957f\u5ea6\u7684\u5bc6\u7801<\/strong><\/h2>\n<p>def generate_password(length):<\/p>\n<p>    password = &#39;&#39;.join(random.choice(characters) for _ in range(length))<\/p>\n<p>    return password<\/p>\n<h2><strong>\u751f\u6210\u5305\u542b\u4e0b\u5212\u7ebf\u7684\u5bc6\u7801<\/strong><\/h2>\n<p>def generate_password_with_underscore(length):<\/p>\n<p>    if length &lt; 1:<\/p>\n<p>        raise ValueError(&quot;Password length must be at least 1&quot;)<\/p>\n<p>    password = &#39;&#39;.join(random.choice(characters) for _ in range(length - 1)) + &#39;_&#39;<\/p>\n<p>    password = list(password)<\/p>\n<p>    random.shuffle(password)<\/p>\n<p>    return &#39;&#39;.join(password)<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a12\u4f4d\u7684\u5bc6\u7801\uff0c\u5e76\u786e\u4fdd\u5305\u542b\u4e0b\u5212\u7ebf<\/strong><\/h2>\n<p>password = generate_password_with_underscore(12)<\/p>\n<p>print(password)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u603b\u7ed3<\/strong>\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u5b57\u7b26\u96c6\u751f\u6210\u968f\u673a\u5b57\u7b26\u3002<\/li>\n<li>\u786e\u4fdd\u4e0b\u5212\u7ebf\u5305\u542b\u5728\u5b57\u7b26\u96c6\u4e2d\u3002<\/li>\n<li>\u4f7f\u7528<code>random.choice<\/code>\u4ece\u5b57\u7b26\u96c6\u4e2d\u9009\u62e9\u5b57\u7b26\u3002<\/li>\n<li>\u786e\u4fdd\u751f\u6210\u7684\u5bc6\u7801\u5305\u542b\u4e0b\u5212\u7ebf\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u5305\u542b\u4e0b\u5212\u7ebf\u7684\u968f\u673a\u5bc6\u7801\uff0c\u5e76\u4e14\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u5bc6\u7801\u7684\u957f\u5ea6\u548c\u5b57\u7b26\u96c6\u3002\u8fd9\u6837\u4f60\u53ef\u4ee5\u786e\u4fdd\u751f\u6210\u7684\u5bc6\u7801\u65e2\u5b89\u5168\u53c8\u7b26\u5408\u8981\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u751f\u6210\u5305\u542b\u4e0b\u5212\u7ebf\u7684\u5bc6\u7801\uff1f<\/strong><br \/>\u8981\u751f\u6210\u5305\u542b\u4e0b\u5212\u7ebf\u7684\u5bc6\u7801\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>random<\/code>\u6a21\u5757\u7ed3\u5408\u5b57\u7b26\u4e32\u5e93\u3002\u901a\u8fc7\u5b9a\u4e49\u5bc6\u7801\u7684\u957f\u5ea6\u548c\u5b57\u7b26\u96c6\uff08\u5305\u62ec\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf\uff09\uff0c\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u7b26\u5408\u6761\u4ef6\u7684\u5bc6\u7801\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import random\nimport string\n\ndef generate_password(length):\n    characters = string.ascii_letters + string.digits + &#39;_&#39;\n    password = &#39;&#39;.join(random.choice(characters) for i in range(length))\n    return password\n\nprint(generate_password(12))  # \u751f\u6210\u4e00\u4e2a12\u4f4d\u7684\u5bc6\u7801\n<\/code><\/pre>\n<p><strong>\u5bc6\u7801\u4e2d\u4e0b\u5212\u7ebf\u7684\u4f7f\u7528\u6709\u4ec0\u4e48\u597d\u5904\uff1f<\/strong><br \/>\u4f7f\u7528\u4e0b\u5212\u7ebf\u4f5c\u4e3a\u5bc6\u7801\u7684\u4e00\u90e8\u5206\uff0c\u53ef\u4ee5\u589e\u52a0\u5bc6\u7801\u7684\u590d\u6742\u6027\u548c\u591a\u6837\u6027\uff0c\u4ece\u800c\u63d0\u9ad8\u5b89\u5168\u6027\u3002\u5bc6\u7801\u7684\u590d\u6742\u6027\u6709\u52a9\u4e8e\u62b5\u6297\u66b4\u529b\u7834\u89e3\u653b\u51fb\u3002\u6b64\u5916\uff0c\u4e0b\u5212\u7ebf\u4f5c\u4e3a\u7279\u6b8a\u5b57\u7b26\u7684\u4f7f\u7528\uff0c\u4f7f\u5f97\u5bc6\u7801\u66f4\u5bb9\u6613\u8bb0\u5fc6\uff0c\u540c\u65f6\u4e5f\u7b26\u5408\u67d0\u4e9b\u7cfb\u7edf\u5bf9\u5bc6\u7801\u590d\u6742\u6027\u7684\u8981\u6c42\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u751f\u6210\u7684\u5bc6\u7801\u5b89\u5168\u4e14\u6613\u4e8e\u8bb0\u5fc6\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u5bc6\u7801\u65e2\u5b89\u5168\u53c8\u5bb9\u6613\u8bb0\u5fc6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u77ed\u8bed\u6216\u53e5\u5b50\u4e2d\u7684\u5355\u8bcd\uff0c\u5076\u5c14\u52a0\u5165\u4e0b\u5212\u7ebf\u548c\u6570\u5b57\u3002\u4f8b\u5982\uff0c\u201cMy_Dog_2023_Rocks\u201d\u8fd9\u6837\u7684\u5bc6\u7801\u65e2\u5305\u542b\u4e0b\u5212\u7ebf\uff0c\u4e5f\u76f8\u5bf9\u5bb9\u6613\u8bb0\u4f4f\u3002\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5bc6\u7801\u7ba1\u7406\u5de5\u5177\u6765\u751f\u6210\u548c\u5b58\u50a8\u590d\u6742\u5bc6\u7801\uff0c\u964d\u4f4e\u8bb0\u5fc6\u8d1f\u62c5\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684\u5bc6\u7801\u662f\u5426\u7b26\u5408\u4e0b\u5212\u7ebf\u8981\u6c42\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u68c0\u67e5\u7528\u6237\u8f93\u5165\u7684\u5bc6\u7801\u662f\u5426\u5305\u542b\u4e0b\u5212\u7ebf\u3002\u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\uff1a<\/p>\n<pre><code class=\"language-python\">import re\n\ndef validate_password(password):\n    pattern = r&#39;^(?=.*_).+$&#39;  # \u81f3\u5c11\u5305\u542b\u4e00\u4e2a\u4e0b\u5212\u7ebf\n    return bool(re.match(pattern, password))\n\nprint(validate_password(&quot;My_Password123&quot;))  # \u8fd4\u56deTrue\nprint(validate_password(&quot;MyPassword123&quot;))    # \u8fd4\u56deFalse\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\u548c\u89e3\u7b54\uff0c\u7528\u6237\u53ef\u4ee5\u66f4\u5168\u9762\u5730\u4e86\u89e3\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u5305\u542b\u4e0b\u5212\u7ebf\u7684\u5bc6\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u751f\u6210\u5305\u542b\u4e0b\u5212\u7ebf\u7684\u5bc6\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528\u968f\u673a\u5b57\u7b26\u751f\u6210\u5668\u6765\u751f\u6210\u4e00\u4e2a\u5305\u542b\u6307\u5b9a\u5b57\u7b26\u96c6\uff08\u5305\u62ec\u4e0b\u5212\u7ebf\uff09\u7684\u5bc6\u7801\u3002\u4f60 [&hellip;]","protected":false},"author":3,"featured_media":1109453,"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\/1109447"}],"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=1109447"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1109447\/revisions"}],"predecessor-version":[{"id":1109458,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1109447\/revisions\/1109458"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1109453"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1109447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1109447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1109447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}