{"id":1101533,"date":"2025-01-08T15:50:40","date_gmt":"2025-01-08T07:50:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1101533.html"},"modified":"2025-01-08T15:50:44","modified_gmt":"2025-01-08T07:50:44","slug":"python%e5%a6%82%e4%bd%95%e8%be%93%e5%85%a5%e4%b8%a4%e4%b8%aa%e5%8f%98%e9%87%8f%e7%9a%84%e5%80%bc-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1101533.html","title":{"rendered":"python\u5982\u4f55\u8f93\u5165\u4e24\u4e2a\u53d8\u91cf\u7684\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064243\/0c984f15-305e-4d8e-962b-972924b9a75e.webp\" alt=\"python\u5982\u4f55\u8f93\u5165\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\" \/><\/p>\n<p><p> <strong>\u5728 Python \u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8f93\u5165\u4e24\u4e2a\u53d8\u91cf\u7684\u503c<\/strong>\uff0c<strong>\u4f7f\u7528\u5185\u5efa\u51fd\u6570 <code>input()<\/code>\u3001\u8bfb\u53d6\u6587\u4ef6\u3001\u547d\u4ee4\u884c\u53c2\u6570<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528 <code>input()<\/code> \u51fd\u6570\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528 <code>input()<\/code> \u51fd\u6570\u8f93\u5165\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u76f8\u5173\u7684\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528 <code>input()<\/code> \u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p><code>input()<\/code> \u51fd\u6570\u7528\u4e8e\u4ece\u6807\u51c6\u8f93\u5165\uff08\u901a\u5e38\u662f\u952e\u76d8\uff09\u83b7\u53d6\u7528\u6237\u8f93\u5165\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\u6765\u8f93\u5165\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f93\u5165\u4e24\u4e2a\u53d8\u91cf\u7684\u503c<\/p>\n<p>x = input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e00\u4e2a\u53d8\u91cf\u7684\u503c: &quot;)<\/p>\n<p>y = input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e8c\u4e2a\u53d8\u91cf\u7684\u503c: &quot;)<\/p>\n<h2><strong>\u8f93\u51fa\u53d8\u91cf\u7684\u503c<\/strong><\/h2>\n<p>print(f&quot;\u7b2c\u4e00\u4e2a\u53d8\u91cf\u7684\u503c\u662f: {x}&quot;)<\/p>\n<p>print(f&quot;\u7b2c\u4e8c\u4e2a\u53d8\u91cf\u7684\u503c\u662f: {y}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>input()<\/code> \u51fd\u6570\u7528\u4e8e\u83b7\u53d6\u7528\u6237\u8f93\u5165\uff0c\u5e76\u5c06\u8f93\u5165\u7684\u503c\u8d4b\u7ed9\u53d8\u91cf <code>x<\/code> \u548c <code>y<\/code>\u3002\u7136\u540e\uff0c\u4f7f\u7528 <code>print()<\/code> \u51fd\u6570\u8f93\u51fa\u8fd9\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>\u8f6c\u6362\u8f93\u5165\u7684\u7c7b\u578b<\/strong><\/p>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>input()<\/code> \u51fd\u6570\u8fd4\u56de\u7684\u503c\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\u3002\u5982\u679c\u4f60\u9700\u8981\u8f93\u5165\u7684\u503c\u662f\u5176\u4ed6\u7c7b\u578b\uff08\u4f8b\u5982\u6574\u6570\u6216\u6d6e\u70b9\u6570\uff09\uff0c\u4f60\u9700\u8981\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f93\u5165\u4e24\u4e2a\u6574\u6570\u53d8\u91cf\u7684\u503c<\/p>\n<p>x = int(input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e00\u4e2a\u6574\u6570\u53d8\u91cf\u7684\u503c: &quot;))<\/p>\n<p>y = int(input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e8c\u4e2a\u6574\u6570\u53d8\u91cf\u7684\u503c: &quot;))<\/p>\n<h2><strong>\u8f93\u51fa\u53d8\u91cf\u7684\u503c<\/strong><\/h2>\n<p>print(f&quot;\u7b2c\u4e00\u4e2a\u6574\u6570\u53d8\u91cf\u7684\u503c\u662f: {x}&quot;)<\/p>\n<p>print(f&quot;\u7b2c\u4e8c\u4e2a\u6574\u6570\u53d8\u91cf\u7684\u503c\u662f: {y}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528 <code>int()<\/code> \u51fd\u6570\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u540c\u6837\u5730\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>float()<\/code> \u51fd\u6570\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528 <code>split()<\/code> \u65b9\u6cd5\u4e00\u6b21\u6027\u8f93\u5165\u591a\u4e2a\u53d8\u91cf<\/strong><\/p>\n<\/p>\n<p><p>\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528 <code>split()<\/code> \u65b9\u6cd5\u4e00\u6b21\u6027\u8f93\u5165\u591a\u4e2a\u53d8\u91cf\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f93\u5165\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\uff0c\u5e76\u7528\u7a7a\u683c\u5206\u9694<\/p>\n<p>x, y = input(&quot;\u8bf7\u8f93\u5165\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\uff08\u7528\u7a7a\u683c\u5206\u9694\uff09: &quot;).split()<\/p>\n<h2><strong>\u8f93\u51fa\u53d8\u91cf\u7684\u503c<\/strong><\/h2>\n<p>print(f&quot;\u7b2c\u4e00\u4e2a\u53d8\u91cf\u7684\u503c\u662f: {x}&quot;)<\/p>\n<p>print(f&quot;\u7b2c\u4e8c\u4e2a\u53d8\u91cf\u7684\u503c\u662f: {y}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7528\u6237\u8f93\u5165\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\uff0c\u5e76\u7528\u7a7a\u683c\u5206\u9694\u3002<code>split()<\/code> \u65b9\u6cd5\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u591a\u4e2a\u90e8\u5206\uff0c\u5e76\u5c06\u8fd9\u4e9b\u90e8\u5206\u8d4b\u7ed9\u53d8\u91cf <code>x<\/code> \u548c <code>y<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>input()<\/code> \u51fd\u6570\u8f93\u5165\u548c\u8f6c\u6362<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528 <code>input()<\/code> \u51fd\u6570\u8f93\u5165\u53d8\u91cf\u503c\u65f6\uff0c\u5e38\u5e38\u9700\u8981\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362<\/strong>\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u9700\u8981\u8f93\u5165\u4e24\u4e2a\u6d6e\u70b9\u6570\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>float()<\/code> \u51fd\u6570\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f93\u5165\u4e24\u4e2a\u6d6e\u70b9\u6570\u53d8\u91cf\u7684\u503c<\/p>\n<p>x = float(input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e00\u4e2a\u6d6e\u70b9\u6570\u53d8\u91cf\u7684\u503c: &quot;))<\/p>\n<p>y = float(input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e8c\u4e2a\u6d6e\u70b9\u6570\u53d8\u91cf\u7684\u503c: &quot;))<\/p>\n<h2><strong>\u8f93\u51fa\u53d8\u91cf\u7684\u503c<\/strong><\/h2>\n<p>print(f&quot;\u7b2c\u4e00\u4e2a\u6d6e\u70b9\u6570\u53d8\u91cf\u7684\u503c\u662f: {x}&quot;)<\/p>\n<p>print(f&quot;\u7b2c\u4e8c\u4e2a\u6d6e\u70b9\u6570\u53d8\u91cf\u7684\u503c\u662f: {y}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u8f93\u5165\u7684\u53d8\u91cf\u662f\u6240\u9700\u7684\u7c7b\u578b<\/strong>\uff0c\u4f8b\u5982\u6574\u6570\u6216\u6d6e\u70b9\u6570\u3002<strong>\u8f6c\u6362\u8f93\u5165\u7c7b\u578b\u662f\u975e\u5e38\u91cd\u8981\u7684<\/strong>\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u65f6\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 <code>split()<\/code> \u65b9\u6cd5\u4e00\u6b21\u6027\u8f93\u5165\u591a\u4e2a\u53d8\u91cf<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528 <code>split()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u4e00\u6b21\u6027\u8f93\u5165\u591a\u4e2a\u53d8\u91cf\u7684\u503c<\/strong>\uff0c\u5e76\u5c06\u5176\u5206\u914d\u7ed9\u591a\u4e2a\u53d8\u91cf\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u5408\u9700\u8981\u540c\u65f6\u8f93\u5165\u591a\u4e2a\u503c\u7684\u573a\u666f<\/strong>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f93\u5165\u4e09\u4e2a\u53d8\u91cf\u7684\u503c\uff0c\u5e76\u7528\u9017\u53f7\u5206\u9694<\/p>\n<p>x, y, z = input(&quot;\u8bf7\u8f93\u5165\u4e09\u4e2a\u53d8\u91cf\u7684\u503c\uff08\u7528\u9017\u53f7\u5206\u9694\uff09: &quot;).split(&#39;,&#39;)<\/p>\n<h2><strong>\u8f93\u51fa\u53d8\u91cf\u7684\u503c<\/strong><\/h2>\n<p>print(f&quot;\u7b2c\u4e00\u4e2a\u53d8\u91cf\u7684\u503c\u662f: {x}&quot;)<\/p>\n<p>print(f&quot;\u7b2c\u4e8c\u4e2a\u53d8\u91cf\u7684\u503c\u662f: {y}&quot;)<\/p>\n<p>print(f&quot;\u7b2c\u4e09\u4e2a\u53d8\u91cf\u7684\u503c\u662f: {z}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7528\u6237\u8f93\u5165\u4e09\u4e2a\u53d8\u91cf\u7684\u503c\uff0c\u5e76\u7528\u9017\u53f7\u5206\u9694\u3002<code>split(&#39;,&#39;)<\/code> \u65b9\u6cd5\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u6309\u9017\u53f7\u62c6\u5206\u6210\u591a\u4e2a\u90e8\u5206\uff0c\u5e76\u5c06\u8fd9\u4e9b\u90e8\u5206\u8d4b\u7ed9\u53d8\u91cf <code>x<\/code>\u3001<code>y<\/code> \u548c <code>z<\/code>\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u65b9\u4fbf\uff0c\u53ef\u4ee5\u5feb\u901f\u8f93\u5165\u591a\u4e2a\u53d8\u91cf\u7684\u503c<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u53d8\u91cf\u503c<\/h3>\n<\/p>\n<p><p><strong>\u9664\u4e86\u4ece\u6807\u51c6\u8f93\u5165\u83b7\u53d6\u53d8\u91cf\u503c\u5916\uff0c\u8fd8\u53ef\u4ee5\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u53d8\u91cf\u503c<\/strong>\u3002\u8fd9\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u7279\u522b\u6709\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u53d8\u91cf\u7684\u503c<\/p>\n<p>with open(&#39;data.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    lines = file.readlines()<\/p>\n<p>    x = lines[0].strip()<\/p>\n<p>    y = lines[1].strip()<\/p>\n<h2><strong>\u8f93\u51fa\u53d8\u91cf\u7684\u503c<\/strong><\/h2>\n<p>print(f&quot;\u7b2c\u4e00\u4e2a\u53d8\u91cf\u7684\u503c\u662f: {x}&quot;)<\/p>\n<p>print(f&quot;\u7b2c\u4e8c\u4e2a\u53d8\u91cf\u7684\u503c\u662f: {y}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u53d8\u91cf\u503c\u5b58\u50a8\u5728\u6587\u4ef6 <code>data.txt<\/code> \u4e2d\u7684\u6bcf\u4e00\u884c\u3002\u4f7f\u7528 <code>readlines()<\/code> \u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u7684\u6240\u6709\u884c\uff0c\u5e76\u5c06\u6bcf\u4e00\u884c\u7684\u503c\u8d4b\u7ed9\u53d8\u91cf <code>x<\/code> \u548c <code>y<\/code>\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e<\/strong>\uff0c\u5982\u914d\u7f6e\u6587\u4ef6\u6216\u6570\u636e\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\u8f93\u5165\u53d8\u91cf\u503c<\/h3>\n<\/p>\n<p><p><strong>\u53e6\u4e00\u79cd\u8f93\u5165\u53d8\u91cf\u503c\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570<\/strong>\u3002\u8fd9\u5728\u7f16\u5199\u811a\u672c\u65f6\u975e\u5e38\u6709\u7528\u3002\u53ef\u4ee5\u4f7f\u7528 <code>sys<\/code> \u6a21\u5757\u6765\u83b7\u53d6\u547d\u4ee4\u884c\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<h2><strong>\u4ece\u547d\u4ee4\u884c\u53c2\u6570\u83b7\u53d6\u53d8\u91cf\u7684\u503c<\/strong><\/h2>\n<p>x = sys.argv[1]<\/p>\n<p>y = sys.argv[2]<\/p>\n<h2><strong>\u8f93\u51fa\u53d8\u91cf\u7684\u503c<\/strong><\/h2>\n<p>print(f&quot;\u7b2c\u4e00\u4e2a\u53d8\u91cf\u7684\u503c\u662f: {x}&quot;)<\/p>\n<p>print(f&quot;\u7b2c\u4e8c\u4e2a\u53d8\u91cf\u7684\u503c\u662f: {y}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528 <code>sys.argv<\/code> \u83b7\u53d6\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u5e76\u5c06\u5176\u8d4b\u7ed9\u53d8\u91cf <code>x<\/code> \u548c <code>y<\/code>\u3002\u4f8b\u5982\uff0c\u8fd0\u884c\u811a\u672c\u65f6\u53ef\u4ee5\u8fd9\u6837\u8f93\u5165\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python script.py 10 20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u9700\u8981\u4ece\u547d\u4ee4\u884c\u4f20\u9012\u53c2\u6570\u7684\u811a\u672c<\/strong>\uff0c\u5982\u6279\u5904\u7406\u4efb\u52a1\u6216\u81ea\u52a8\u5316\u811a\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528 <code>argparse<\/code> \u6a21\u5757\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570<\/h3>\n<\/p>\n<p><p><strong>\u4e3a\u4e86\u66f4\u5f3a\u5927\u7684\u547d\u4ee4\u884c\u53c2\u6570\u5904\u7406\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>argparse<\/code> \u6a21\u5757<\/strong>\u3002<code>argparse<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\u548c\u9009\u9879\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u547d\u4ee4\u884c\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import argparse<\/p>\n<h2><strong>\u521b\u5efa\u89e3\u6790\u5668<\/strong><\/h2>\n<p>parser = argparse.ArgumentParser(description=&quot;\u8f93\u5165\u4e24\u4e2a\u53d8\u91cf\u7684\u503c&quot;)<\/p>\n<h2><strong>\u6dfb\u52a0\u53c2\u6570<\/strong><\/h2>\n<p>parser.add_argument(&#39;x&#39;, type=int, help=&quot;\u7b2c\u4e00\u4e2a\u53d8\u91cf\u7684\u503c&quot;)<\/p>\n<p>parser.add_argument(&#39;y&#39;, type=int, help=&quot;\u7b2c\u4e8c\u4e2a\u53d8\u91cf\u7684\u503c&quot;)<\/p>\n<h2><strong>\u89e3\u6790\u53c2\u6570<\/strong><\/h2>\n<p>args = parser.parse_args()<\/p>\n<h2><strong>\u8f93\u51fa\u53d8\u91cf\u7684\u503c<\/strong><\/h2>\n<p>print(f&quot;\u7b2c\u4e00\u4e2a\u53d8\u91cf\u7684\u503c\u662f: {args.x}&quot;)<\/p>\n<p>print(f&quot;\u7b2c\u4e8c\u4e2a\u53d8\u91cf\u7684\u503c\u662f: {args.y}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528 <code>argparse<\/code> \u6a21\u5757\u521b\u5efa\u89e3\u6790\u5668\uff0c\u5e76\u6dfb\u52a0\u4e24\u4e2a\u6574\u6570\u53c2\u6570\u3002\u7136\u540e\uff0c\u4f7f\u7528 <code>parse_args()<\/code> \u65b9\u6cd5\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u5e76\u8f93\u51fa\u53d8\u91cf\u7684\u503c\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u547d\u4ee4\u884c\u53c2\u6570\u5904\u7406\u529f\u80fd<\/strong>\uff0c\u5982\u5e2e\u52a9\u4fe1\u606f\u548c\u7c7b\u578b\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4ece\u73af\u5883\u53d8\u91cf\u83b7\u53d6\u53d8\u91cf\u503c<\/h3>\n<\/p>\n<p><p><strong>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4ece\u73af\u5883\u53d8\u91cf\u4e2d\u83b7\u53d6\u53d8\u91cf\u7684\u503c<\/strong>\u3002\u8fd9\u5728\u5904\u7406\u914d\u7f6e\u6216\u654f\u611f\u4fe1\u606f\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u4ece\u73af\u5883\u53d8\u91cf\u83b7\u53d6\u53d8\u91cf\u7684\u503c<\/strong><\/h2>\n<p>x = os.getenv(&#39;VAR1&#39;)<\/p>\n<p>y = os.getenv(&#39;VAR2&#39;)<\/p>\n<h2><strong>\u8f93\u51fa\u53d8\u91cf\u7684\u503c<\/strong><\/h2>\n<p>print(f&quot;\u7b2c\u4e00\u4e2a\u53d8\u91cf\u7684\u503c\u662f: {x}&quot;)<\/p>\n<p>print(f&quot;\u7b2c\u4e8c\u4e2a\u53d8\u91cf\u7684\u503c\u662f: {y}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528 <code>os.getenv()<\/code> \u65b9\u6cd5\u4ece\u73af\u5883\u53d8\u91cf\u4e2d\u83b7\u53d6\u53d8\u91cf\u7684\u503c\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u4ece\u73af\u5883\u53d8\u91cf\u4e2d\u8bfb\u53d6\u914d\u7f6e\u6216\u654f\u611f\u4fe1\u606f<\/strong>\uff0c\u5982 API \u5bc6\u94a5\u6216\u6570\u636e\u5e93\u5bc6\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4ece\u7f51\u7edc\u8bf7\u6c42\u83b7\u53d6\u53d8\u91cf\u503c<\/h3>\n<\/p>\n<p><p><strong>\u5728\u67d0\u4e9b\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7f51\u7edc\u8bf7\u6c42\u83b7\u53d6\u53d8\u91cf\u7684\u503c<\/strong>\u3002\u4f8b\u5982\uff0c\u4ece API \u83b7\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u4ece API \u83b7\u53d6\u53d8\u91cf\u7684\u503c<\/strong><\/h2>\n<p>response = requests.get(&#39;https:\/\/api.example.com\/data&#39;)<\/p>\n<p>data = response.json()<\/p>\n<p>x = data[&#39;var1&#39;]<\/p>\n<p>y = data[&#39;var2&#39;]<\/p>\n<h2><strong>\u8f93\u51fa\u53d8\u91cf\u7684\u503c<\/strong><\/h2>\n<p>print(f&quot;\u7b2c\u4e00\u4e2a\u53d8\u91cf\u7684\u503c\u662f: {x}&quot;)<\/p>\n<p>print(f&quot;\u7b2c\u4e8c\u4e2a\u53d8\u91cf\u7684\u503c\u662f: {y}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528 <code>requests<\/code> \u5e93\u4ece API \u83b7\u53d6\u6570\u636e\uff0c\u5e76\u5c06\u6570\u636e\u89e3\u6790\u4e3a JSON \u683c\u5f0f\u3002\u7136\u540e\uff0c\u4ece JSON \u6570\u636e\u4e2d\u83b7\u53d6\u53d8\u91cf <code>x<\/code> \u548c <code>y<\/code> \u7684\u503c\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u4ece\u7f51\u7edc\u8bf7\u6c42\u4e2d\u83b7\u53d6\u6570\u636e<\/strong>\uff0c\u5982\u8c03\u7528 RESTful API\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4ece\u6570\u636e\u5e93\u8bfb\u53d6\u53d8\u91cf\u503c<\/h3>\n<\/p>\n<p><p><strong>\u5728\u5904\u7406\u6570\u636e\u65f6\uff0c\u5e38\u5e38\u9700\u8981\u4ece\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u53d8\u91cf\u7684\u503c<\/strong>\u3002\u4f8b\u5982\uff0c\u4ece MySQL \u6570\u636e\u5e93\u8bfb\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mysql.connector<\/p>\n<h2><strong>\u8fde\u63a5\u5230\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = mysql.connector.connect(<\/p>\n<p>    host=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;yourusername&#39;,<\/p>\n<p>    password=&#39;yourpassword&#39;,<\/p>\n<p>    database=&#39;yourdatabase&#39;<\/p>\n<p>)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u6267\u884c\u67e5\u8be2<\/strong><\/h2>\n<p>cursor.execute(&quot;SELECT var1, var2 FROM yourtable LIMIT 1&quot;)<\/p>\n<p>result = cursor.fetchone()<\/p>\n<p>x = result[0]<\/p>\n<p>y = result[1]<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<h2><strong>\u8f93\u51fa\u53d8\u91cf\u7684\u503c<\/strong><\/h2>\n<p>print(f&quot;\u7b2c\u4e00\u4e2a\u53d8\u91cf\u7684\u503c\u662f: {x}&quot;)<\/p>\n<p>print(f&quot;\u7b2c\u4e8c\u4e2a\u53d8\u91cf\u7684\u503c\u662f: {y}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528 <code>mysql.connector<\/code> \u5e93\u8fde\u63a5\u5230 MySQL \u6570\u636e\u5e93\uff0c\u5e76\u6267\u884c\u67e5\u8be2\u4ee5\u83b7\u53d6\u53d8\u91cf <code>x<\/code> \u548c <code>y<\/code> \u7684\u503c\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u4ece\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u6570\u636e<\/strong>\uff0c\u5982\u67e5\u8be2\u8bb0\u5f55\u6216\u914d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4ece\u914d\u7f6e\u6587\u4ef6\u8bfb\u53d6\u53d8\u91cf\u503c<\/h3>\n<\/p>\n<p><p><strong>\u5728\u8bb8\u591a\u5e94\u7528\u4e2d\uff0c\u914d\u7f6e\u6587\u4ef6\u7528\u4e8e\u5b58\u50a8\u53d8\u91cf\u7684\u503c<\/strong>\u3002\u4f8b\u5982\uff0c\u4ece YAML \u914d\u7f6e\u6587\u4ef6\u8bfb\u53d6\u53d8\u91cf\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yaml<\/p>\n<h2><strong>\u4ece\u914d\u7f6e\u6587\u4ef6\u8bfb\u53d6\u53d8\u91cf\u7684\u503c<\/strong><\/h2>\n<p>with open(&#39;config.yaml&#39;, &#39;r&#39;) as file:<\/p>\n<p>    config = yaml.<a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_load(file)<\/p>\n<p>    x = config[&#39;var1&#39;]<\/p>\n<p>    y = config[&#39;var2&#39;]<\/p>\n<h2><strong>\u8f93\u51fa\u53d8\u91cf\u7684\u503c<\/strong><\/h2>\n<p>print(f&quot;\u7b2c\u4e00\u4e2a\u53d8\u91cf\u7684\u503c\u662f: {x}&quot;)<\/p>\n<p>print(f&quot;\u7b2c\u4e8c\u4e2a\u53d8\u91cf\u7684\u503c\u662f: {y}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528 <code>yaml<\/code> \u5e93\u8bfb\u53d6 YAML \u914d\u7f6e\u6587\u4ef6\uff0c\u5e76\u4ece\u914d\u7f6e\u6587\u4ef6\u4e2d\u83b7\u53d6\u53d8\u91cf <code>x<\/code> \u548c <code>y<\/code> \u7684\u503c\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u4ece\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bfb\u53d6\u914d\u7f6e<\/strong>\uff0c\u5982\u5e94\u7528\u7a0b\u5e8f\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4ece JSON \u6587\u4ef6\u8bfb\u53d6\u53d8\u91cf\u503c<\/h3>\n<\/p>\n<p><p><strong>JSON \u6587\u4ef6\u4e5f\u662f\u5e38\u89c1\u7684\u914d\u7f6e\u6587\u4ef6\u683c\u5f0f<\/strong>\u3002\u4f8b\u5982\uff0c\u4ece JSON \u914d\u7f6e\u6587\u4ef6\u8bfb\u53d6\u53d8\u91cf\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u4ece\u914d\u7f6e\u6587\u4ef6\u8bfb\u53d6\u53d8\u91cf\u7684\u503c<\/strong><\/h2>\n<p>with open(&#39;config.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>    config = json.load(file)<\/p>\n<p>    x = config[&#39;var1&#39;]<\/p>\n<p>    y = config[&#39;var2&#39;]<\/p>\n<h2><strong>\u8f93\u51fa\u53d8\u91cf\u7684\u503c<\/strong><\/h2>\n<p>print(f&quot;\u7b2c\u4e00\u4e2a\u53d8\u91cf\u7684\u503c\u662f: {x}&quot;)<\/p>\n<p>print(f&quot;\u7b2c\u4e8c\u4e2a\u53d8\u91cf\u7684\u503c\u662f: {y}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528 <code>json<\/code> \u5e93\u8bfb\u53d6 JSON \u914d\u7f6e\u6587\u4ef6\uff0c\u5e76\u4ece\u914d\u7f6e\u6587\u4ef6\u4e2d\u83b7\u53d6\u53d8\u91cf <code>x<\/code> \u548c <code>y<\/code> \u7684\u503c\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u4ece JSON \u6587\u4ef6\u4e2d\u8bfb\u53d6\u914d\u7f6e\u6216\u6570\u636e<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u793a\u4f8b\uff0c\u53ef\u4ee5\u770b\u5230\u5728 Python \u4e2d\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8f93\u5165\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\uff0c\u5305\u62ec\u4f7f\u7528 <code>input()<\/code> \u51fd\u6570\u3001<code>split()<\/code> \u65b9\u6cd5\u3001\u4ece\u6587\u4ef6\u3001\u547d\u4ee4\u884c\u53c2\u6570\u3001\u73af\u5883\u53d8\u91cf\u3001\u7f51\u7edc\u8bf7\u6c42\u3001\u6570\u636e\u5e93\u3001\u914d\u7f6e\u6587\u4ef6\u7b49\u9014\u5f84\u83b7\u53d6\u53d8\u91cf\u7684\u503c\u3002<strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f<\/strong>\uff0c\u4f8b\u5982\u4ece\u6807\u51c6\u8f93\u5165\u83b7\u53d6\u7528\u6237\u8f93\u5165\uff0c\u6216\u4ece\u914d\u7f6e\u6587\u4ef6\u8bfb\u53d6\u5e94\u7528\u8bbe\u7f6e\u3002\u5e0c\u671b\u8fd9\u4e9b\u793a\u4f8b\u80fd\u5e2e\u52a9\u4f60\u5728\u4e0d\u540c\u7684\u573a\u666f\u4e2d\u5904\u7406\u53d8\u91cf\u7684\u8f93\u5165\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u540c\u65f6\u8f93\u5165\u591a\u4e2a\u53d8\u91cf\u7684\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u503c\u3002\u5982\u679c\u60f3\u8981\u540c\u65f6\u8f93\u5165\u591a\u4e2a\u53d8\u91cf\u7684\u503c\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u9017\u53f7\u5206\u9694\u8f93\u5165\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">a, b = input(&quot;\u8bf7\u8f93\u5165\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\uff0c\u7528\u7a7a\u683c\u9694\u5f00\uff1a&quot;).split()\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u63d0\u793a\u7528\u6237\u8f93\u5165\u4e24\u4e2a\u503c\uff0c\u5e76\u5c06\u5b83\u4eec\u5206\u522b\u8d4b\u7ed9\u53d8\u91cf<code>a<\/code>\u548c<code>b<\/code>\u3002<\/p>\n<p><strong>\u5728\u8f93\u5165\u53d8\u91cf\u65f6\u5982\u4f55\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u7c7b\u578b\u6b63\u786e\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u7c7b\u578b\u7b26\u5408\u8981\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528\u7c7b\u578b\u8f6c\u6362\u51fd\u6570\uff0c\u4f8b\u5982<code>int()<\/code>\u6216<code>float()<\/code>\uff0c\u5bf9\u8f93\u5165\u8fdb\u884c\u5904\u7406\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">a, b = map(int, input(&quot;\u8bf7\u8f93\u5165\u4e24\u4e2a\u6574\u6570\uff0c\u7528\u7a7a\u683c\u9694\u5f00\uff1a&quot;).split())\n<\/code><\/pre>\n<p>\u4f7f\u7528<code>map()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u786e\u4fdd\u53d8\u91cf<code>a<\/code>\u548c<code>b<\/code>\u662f\u6574\u6570\u7c7b\u578b\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u9519\u8bef\u6216\u5f02\u5e38\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u83b7\u53d6\u7528\u6237\u8f93\u5165\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u7ed3\u6784\u6765\u5904\u7406\u53ef\u80fd\u53d1\u751f\u7684\u5f02\u5e38\u3002\u4f8b\u5982\uff0c\u5982\u679c\u7528\u6237\u8f93\u5165\u4e86\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u53ef\u4ee5\u63d0\u793a\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002\u4ee3\u7801\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">while True:\n    try:\n        a, b = map(int, input(&quot;\u8bf7\u8f93\u5165\u4e24\u4e2a\u6574\u6570\uff0c\u7528\u7a7a\u683c\u9694\u5f00\uff1a&quot;).split())\n        break  # \u8f93\u5165\u6b63\u786e\uff0c\u9000\u51fa\u5faa\u73af\n    except ValueError:\n        print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u786e\u4fdd\u8f93\u5165\u7684\u662f\u4e24\u4e2a\u6574\u6570\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u7684\u5904\u7406\u65b9\u5f0f\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\uff0c\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u6709\u6548\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728 Python \u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8f93\u5165\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\uff0c\u4f7f\u7528\u5185\u5efa\u51fd\u6570 input()\u3001\u8bfb\u53d6\u6587\u4ef6\u3001\u547d\u4ee4\u884c\u53c2\u6570\u3002\u5176\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1101552,"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\/1101533"}],"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=1101533"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1101533\/revisions"}],"predecessor-version":[{"id":1101554,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1101533\/revisions\/1101554"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1101552"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1101533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1101533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1101533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}