{"id":1153150,"date":"2025-01-13T17:35:16","date_gmt":"2025-01-13T09:35:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1153150.html"},"modified":"2025-01-13T17:35:18","modified_gmt":"2025-01-13T09:35:18","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e8%be%93%e5%85%a5%e7%a9%ba%e6%a0%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1153150.html","title":{"rendered":"python\u5982\u4f55\u5b9e\u73b0\u8f93\u5165\u7a7a\u683c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25182946\/38c283ce-ffa0-40c4-a3af-4bb53fa54196.webp\" alt=\"python\u5982\u4f55\u5b9e\u73b0\u8f93\u5165\u7a7a\u683c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5b9e\u73b0\u8f93\u5165\u7a7a\u683c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528input()\u51fd\u6570\u3001\u4f7f\u7528sys.stdin.read()\u51fd\u6570\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u4ee5\u53ca\u901a\u8fc7GUI\u6846\u67b6\u5b9e\u73b0\u3002<\/strong>\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u7ed9\u51fa\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528input()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684input()\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u65b9\u6cd5\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cinput()\u51fd\u6570\u4f1a\u622a\u53d6\u7528\u6237\u8f93\u5165\u7684\u6574\u884c\u5185\u5bb9\uff0c\u5305\u62ec\u7a7a\u683c\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">user_input = input(&quot;Please enter something: &quot;)<\/p>\n<p>print(f&quot;You entered: {user_input}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7528\u6237\u53ef\u4ee5\u8f93\u5165\u4efb\u4f55\u5185\u5bb9\uff0c\u5305\u62ec\u7a7a\u683c\u3002input()\u51fd\u6570\u5c06\u6355\u83b7\u6574\u884c\u8f93\u5165\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8fd4\u56de\u3002<\/p>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p>input()\u51fd\u6570\u662f\u6700\u7b80\u5355\u3001\u6700\u76f4\u63a5\u7684\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u65b9\u6cd5\u3002\u5b83\u4f1a\u7b49\u5f85\u7528\u6237\u8f93\u5165\uff0c\u76f4\u5230\u7528\u6237\u6309\u4e0b\u56de\u8f66\u952e\u4e3a\u6b62\u3002\u6240\u6709\u8f93\u5165\u7684\u5b57\u7b26\uff0c\u5305\u62ec\u7a7a\u683c\uff0c\u90fd\u5c06\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8fd4\u56de\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cinput()\u51fd\u6570\u8fd4\u56de\u7684\u59cb\u7ec8\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u5982\u679c\u9700\u8981\u5176\u4ed6\u7c7b\u578b\u7684\u6570\u636e\uff0c\u9700\u8981\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528sys.stdin.read()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>sys.stdin.read()\u51fd\u6570\u7528\u4e8e\u4ece\u6807\u51c6\u8f93\u5165\u8bfb\u53d6\u6570\u636e\uff0c\u9002\u7528\u4e8e\u9700\u8981\u591a\u884c\u8f93\u5165\u7684\u60c5\u51b5\u3002\u5b83\u4e5f\u80fd\u8bfb\u53d6\u5305\u542b\u7a7a\u683c\u7684\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>print(&quot;Please enter some text (press Ctrl+D to end):&quot;)<\/p>\n<p>user_input = sys.stdin.read()<\/p>\n<p>print(f&quot;You entered: {user_input}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7528\u6237\u53ef\u4ee5\u8f93\u5165\u591a\u884c\u6587\u672c\uff0c\u5305\u62ec\u7a7a\u683c\u3002\u6309\u4e0bCtrl+D\uff08Unix\/Linux\/Mac\uff09\u6216Ctrl+Z\uff08Windows\uff09\u7ed3\u675f\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p>sys.stdin.read()\u51fd\u6570\u9002\u5408\u5904\u7406\u5927\u5757\u6587\u672c\u8f93\u5165\uff0c\u5c24\u5176\u662f\u5f53\u9700\u8981\u5904\u7406\u591a\u884c\u8f93\u5165\u65f6\u3002\u4e0einput()\u4e0d\u540c\uff0csys.stdin.read()\u4e0d\u4f1a\u5728\u9047\u5230\u56de\u8f66\u952e\u65f6\u7ed3\u675f\u8f93\u5165\uff0c\u800c\u662f\u7b49\u5f85EOF\uff08\u6587\u4ef6\u7ed3\u675f\u7b26\uff09\u3002\u8fd9\u79cd\u65b9\u6cd5\u5bf9\u4e8e\u67d0\u4e9b\u9700\u8981\u5927\u91cf\u6587\u672c\u8f93\u5165\u7684\u5e94\u7528\u573a\u666f\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u5bf9\u7528\u6237\u8f93\u5165\u8fdb\u884c\u66f4\u4e3a\u7cbe\u7ec6\u7684\u63a7\u5236\u548c\u9a8c\u8bc1\u3002\u53ef\u4ee5\u4f7f\u7528re\u6a21\u5757\u6765\u5904\u7406\u5305\u542b\u7a7a\u683c\u7684\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>user_input = input(&quot;Please enter something: &quot;)<\/p>\n<p>if re.match(r&#39;^\\s*$&#39;, user_input):<\/p>\n<p>    print(&quot;Input cont<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ns only spaces.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;You entered: {user_input}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u7528\u4e8e\u68c0\u67e5\u7528\u6237\u8f93\u5165\u662f\u5426\u4ec5\u5305\u542b\u7a7a\u683c\u3002<\/p>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5339\u914d\u548c\u64cd\u4f5c\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u8f93\u5165\u8fdb\u884c\u590d\u6742\u9a8c\u8bc1\u548c\u5904\u7406\u7684\u60c5\u51b5\u3002\u901a\u8fc7re\u6a21\u5757\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u5bf9\u5305\u542b\u7a7a\u683c\u7684\u8f93\u5165\u7684\u5404\u79cd\u64cd\u4f5c\uff0c\u5982\u5339\u914d\u3001\u66ff\u6362\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u901a\u8fc7GUI\u6846\u67b6\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528GUI\u6846\u67b6\uff08\u5982Tkinter\uff09\u53ef\u4ee5\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\uff0c\u63a5\u6536\u7528\u6237\u8f93\u5165\uff0c\u5305\u62ec\u7a7a\u683c\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def on_submit():<\/p>\n<p>    user_input = entry.get()<\/p>\n<p>    print(f&quot;You entered: {user_input}&quot;)<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Input Example&quot;)<\/p>\n<p>label = tk.Label(root, text=&quot;Please enter something:&quot;)<\/p>\n<p>label.pack()<\/p>\n<p>entry = tk.Entry(root)<\/p>\n<p>entry.pack()<\/p>\n<p>button = tk.Button(root, text=&quot;Submit&quot;, command=on_submit)<\/p>\n<p>button.pack()<\/p>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528Tkinter\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u5f62\u754c\u9762\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u6587\u672c\u6846\u4e2d\u8f93\u5165\u5185\u5bb9\uff0c\u5305\u62ec\u7a7a\u683c\u3002<\/p>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p>GUI\u6846\u67b6\u63d0\u4f9b\u4e86\u66f4\u53cb\u597d\u548c\u76f4\u89c2\u7684\u7528\u6237\u4ea4\u4e92\u65b9\u5f0f\u3002\u901a\u8fc7\u4f7f\u7528Tkinter\u3001PyQt\u7b49\u5e93\uff0c\u53ef\u4ee5\u521b\u5efa\u529f\u80fd\u4e30\u5bcc\u7684\u56fe\u5f62\u7528\u6237\u754c\u9762\uff0c\u63a5\u6536\u5404\u79cd\u5f62\u5f0f\u7684\u7528\u6237\u8f93\u5165\u3002GUI\u6846\u67b6\u9002\u7528\u4e8e\u9700\u8981\u590d\u6742\u7528\u6237\u4ea4\u4e92\u7684\u5e94\u7528\uff0c\u5982\u684c\u9762\u8f6f\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528getpass\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u7528\u6237\u8f93\u5165\u5bc6\u7801\u6216\u5176\u4ed6\u654f\u611f\u4fe1\u606f\uff0cgetpass\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u5b89\u5168\u7684\u8f93\u5165\u65b9\u6cd5\uff0c\u4e0d\u4f1a\u5728\u5c4f\u5e55\u4e0a\u663e\u793a\u8f93\u5165\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import getpass<\/p>\n<p>user_input = getpass.getpass(&quot;Please enter your password: &quot;)<\/p>\n<p>print(f&quot;You entered: {user_input}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7528\u6237\u8f93\u5165\u7684\u5bc6\u7801\u4e0d\u4f1a\u5728\u5c4f\u5e55\u4e0a\u663e\u793a\uff0c\u4f46\u4ecd\u7136\u53ef\u4ee5\u5305\u542b\u7a7a\u683c\u3002<\/p>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p>getpass\u6a21\u5757\u9002\u7528\u4e8e\u9700\u8981\u4fdd\u62a4\u7528\u6237\u8f93\u5165\u9690\u79c1\u7684\u573a\u666f\uff0c\u5982\u8f93\u5165\u5bc6\u7801\u7b49\u654f\u611f\u4fe1\u606f\u3002\u867d\u7136\u8f93\u5165\u5185\u5bb9\u4e0d\u4f1a\u5728\u5c4f\u5e55\u4e0a\u663e\u793a\uff0c\u4f46\u4ecd\u7136\u53ef\u4ee5\u6b63\u5e38\u6355\u83b7\u5305\u542b\u7a7a\u683c\u7684\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528command-line\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u4f20\u9012\u5305\u542b\u7a7a\u683c\u7684\u8f93\u5165\uff0c\u4f7f\u7528sys.argv\u6765\u83b7\u53d6\u8fd9\u4e9b\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>if len(sys.argv) &gt; 1:<\/p>\n<p>    user_input = &#39; &#39;.join(sys.argv[1:])<\/p>\n<p>    print(f&quot;You entered: {user_input}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;No input provided.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u4f20\u9012\u8f93\u5165\uff0c\u5305\u62ec\u7a7a\u683c\u3002<\/p>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p>\u547d\u4ee4\u884c\u53c2\u6570\u63d0\u4f9b\u4e86\u4e00\u79cd\u7075\u6d3b\u7684\u8f93\u5165\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u9700\u8981\u4ece\u547d\u4ee4\u884c\u4f20\u9012\u8f93\u5165\u7684\u811a\u672c\u548c\u5de5\u5177\u3002\u901a\u8fc7sys.argv\uff0c\u53ef\u4ee5\u83b7\u53d6\u5305\u542b\u7a7a\u683c\u7684\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u5e76\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u6587\u4ef6\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u5e94\u7528\u573a\u666f\u4e0b\uff0c\u53ef\u4ee5\u5c06\u8f93\u5165\u5199\u5165\u6587\u4ef6\uff0c\u7136\u540e\u901a\u8fc7Python\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u8fd9\u79cd\u65b9\u6cd5\u540c\u6837\u53ef\u4ee5\u5904\u7406\u5305\u542b\u7a7a\u683c\u7684\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># Writing to a file<\/p>\n<p>with open(&#39;input.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&quot;This is a test input with spaces.&quot;)<\/p>\n<h2><strong>Reading from a file<\/strong><\/h2>\n<p>with open(&#39;input.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    user_input = file.read()<\/p>\n<p>    print(f&quot;You entered: {user_input}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8f93\u5165\u88ab\u5199\u5165\u6587\u4ef6\uff0c\u7136\u540e\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\uff0c\u5305\u62ec\u7a7a\u683c\u3002<\/p>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p>\u6587\u4ef6\u8f93\u5165\u8f93\u51fa\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u6216\u6301\u4e45\u5316\u6570\u636e\u7684\u5e94\u7528\u573a\u666f\u3002\u901a\u8fc7\u6587\u4ef6\u64cd\u4f5c\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u5904\u7406\u5404\u79cd\u5f62\u5f0f\u7684\u8f93\u5165\uff0c\u5305\u62ec\u5305\u542b\u7a7a\u683c\u7684\u6587\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528JSON\u683c\u5f0f\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u4ea4\u6362\u548c\u5b58\u50a8\u4e2d\uff0cJSON\u662f\u4e00\u79cd\u5e38\u7528\u683c\u5f0f\u3002\u53ef\u4ee5\u5c06\u8f93\u5165\u6570\u636e\u683c\u5f0f\u5316\u4e3aJSON\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u901a\u8fc7Python\u89e3\u6790\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>Example JSON input<\/strong><\/h2>\n<p>json_input = &#39;{&quot;message&quot;: &quot;This is a test input with spaces.&quot;}&#39;<\/p>\n<h2><strong>Parsing JSON input<\/strong><\/h2>\n<p>data = json.loads(json_input)<\/p>\n<p>print(f&quot;You entered: {data[&#39;message&#39;]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8f93\u5165\u6570\u636e\u88ab\u683c\u5f0f\u5316\u4e3aJSON\u5b57\u7b26\u4e32\uff0c\u5e76\u901a\u8fc7json\u6a21\u5757\u8fdb\u884c\u89e3\u6790\u3002<\/p>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p>JSON\u683c\u5f0f\u8f93\u5165\u9002\u7528\u4e8e\u9700\u8981\u4f20\u9012\u7ed3\u6784\u5316\u6570\u636e\u7684\u573a\u666f\uff0c\u5982API\u63a5\u53e3\u3001\u914d\u7f6e\u6587\u4ef6\u7b49\u3002\u901a\u8fc7json\u6a21\u5757\uff0c\u53ef\u4ee5\u8f7b\u677e\u89e3\u6790\u548c\u751f\u6210JSON\u6570\u636e\uff0c\u5904\u7406\u5305\u542b\u7a7a\u683c\u7684\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u73af\u5883\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u4f20\u9012\u8f93\u5165\uff0c\u5305\u62ec\u7a7a\u683c\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528os.environ\u83b7\u53d6\u73af\u5883\u53d8\u91cf\u503c\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>user_input = os.getenv(&#39;USER_INPUT&#39;, &#39;No input provided&#39;)<\/p>\n<p>print(f&quot;You entered: {user_input}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8f93\u5165\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u4f20\u9012\uff0c\u5e76\u5728Python\u4e2d\u83b7\u53d6\u3002<\/p>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p>\u73af\u5883\u53d8\u91cf\u63d0\u4f9b\u4e86\u4e00\u79cd\u7075\u6d3b\u7684\u8f93\u5165\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u9700\u8981\u4ece\u64cd\u4f5c\u7cfb\u7edf\u73af\u5883\u4f20\u9012\u8f93\u5165\u7684\u573a\u666f\u3002\u901a\u8fc7os.environ\uff0c\u53ef\u4ee5\u83b7\u53d6\u5305\u542b\u7a7a\u683c\u7684\u73af\u5883\u53d8\u91cf\u503c\uff0c\u5e76\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u5e94\u7528\u4e2d\uff0c\u8f93\u5165\u6570\u636e\u53ef\u4ee5\u5b58\u50a8\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u901a\u8fc7Python\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u5185\u5bb9\u3002\u8fd9\u79cd\u65b9\u6cd5\u540c\u6837\u53ef\u4ee5\u5904\u7406\u5305\u542b\u7a7a\u683c\u7684\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import configparser<\/p>\n<h2><strong>Example configuration file (config.ini)<\/strong><\/h2>\n<h2><strong>[DEFAULT]<\/strong><\/h2>\n<h2><strong>message = This is a test input with spaces.<\/strong><\/h2>\n<p>config = configparser.ConfigParser()<\/p>\n<p>config.read(&#39;config.ini&#39;)<\/p>\n<p>user_input = config[&#39;DEFAULT&#39;][&#39;message&#39;]<\/p>\n<p>print(f&quot;You entered: {user_input}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8f93\u5165\u6570\u636e\u5b58\u50a8\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u5e76\u901a\u8fc7configparser\u6a21\u5757\u8fdb\u884c\u8bfb\u53d6\u3002<\/p>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p>\u914d\u7f6e\u6587\u4ef6\u9002\u7528\u4e8e\u9700\u8981\u6301\u4e45\u5316\u914d\u7f6e\u6570\u636e\u7684\u5e94\u7528\u573a\u666f\u3002\u901a\u8fc7configparser\u7b49\u6a21\u5757\uff0c\u53ef\u4ee5\u8f7b\u677e\u8bfb\u53d6\u548c\u89e3\u6790\u914d\u7f6e\u6587\u4ef6\uff0c\u5904\u7406\u5305\u542b\u7a7a\u683c\u7684\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u4f7f\u7528\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u590d\u6742\u5e94\u7528\u4e2d\uff0c\u8f93\u5165\u6570\u636e\u53ef\u4ee5\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\uff0c\u7136\u540e\u901a\u8fc7Python\u8fdb\u884c\u67e5\u8be2\u548c\u5904\u7406\u3002\u8fd9\u79cd\u65b9\u6cd5\u540c\u6837\u53ef\u4ee5\u5904\u7406\u5305\u542b\u7a7a\u683c\u7684\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>Creating a database connection<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>Creating a table<\/strong><\/h2>\n<p>cursor.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS input_data (message TEXT)&#39;&#39;&#39;)<\/p>\n<h2><strong>Inserting data<\/strong><\/h2>\n<p>cursor.execute(&quot;INSERT INTO input_data (message) VALUES (&#39;This is a test input with spaces.&#39;)&quot;)<\/p>\n<p>conn.commit()<\/p>\n<h2><strong>Querying data<\/strong><\/h2>\n<p>cursor.execute(&quot;SELECT message FROM input_data&quot;)<\/p>\n<p>user_input = cursor.fetchone()[0]<\/p>\n<p>print(f&quot;You entered: {user_input}&quot;)<\/p>\n<h2><strong>Closing the connection<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8f93\u5165\u6570\u636e\u5b58\u50a8\u5728SQLite\u6570\u636e\u5e93\u4e2d\uff0c\u5e76\u901a\u8fc7SQL\u67e5\u8be2\u8fdb\u884c\u8bfb\u53d6\u3002<\/p>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p>\u6570\u636e\u5e93\u9002\u7528\u4e8e\u9700\u8981\u5b58\u50a8\u548c\u7ba1\u7406\u5927\u91cf\u6570\u636e\u7684\u5e94\u7528\u573a\u666f\u3002\u901a\u8fc7SQLite\u3001MySQL\u7b49\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u5b58\u50a8\u548c\u67e5\u8be2\u5305\u542b\u7a7a\u683c\u7684\u8f93\u5165\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u4f7f\u7528\u7f51\u7edc\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u7f51\u7edc\u5e94\u7528\u4e2d\uff0c\u8f93\u5165\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u7f51\u7edc\u4f20\u8f93\uff0c\u7136\u540e\u5728Python\u4e2d\u8fdb\u884c\u5904\u7406\u3002\u8fd9\u79cd\u65b9\u6cd5\u540c\u6837\u53ef\u4ee5\u5904\u7406\u5305\u542b\u7a7a\u683c\u7684\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<h2><strong>Creating a socket<\/strong><\/h2>\n<p>server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<p>server_socket.bind((&#39;localhost&#39;, 8080))<\/p>\n<p>server_socket.listen(1)<\/p>\n<p>print(&quot;Waiting for a connection...&quot;)<\/p>\n<p>connection, address = server_socket.accept()<\/p>\n<h2><strong>Receiving data<\/strong><\/h2>\n<p>data = connection.recv(1024).decode(&#39;utf-8&#39;)<\/p>\n<p>print(f&quot;You entered: {data}&quot;)<\/p>\n<h2><strong>Closing the connection<\/strong><\/h2>\n<p>connection.close()<\/p>\n<p>server_socket.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8f93\u5165\u6570\u636e\u901a\u8fc7\u7f51\u7edc\u4f20\u8f93\uff0c\u5e76\u5728\u670d\u52a1\u5668\u7aef\u8fdb\u884c\u63a5\u6536\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p>\u7f51\u7edc\u8f93\u5165\u9002\u7528\u4e8e\u9700\u8981\u901a\u8fc7\u7f51\u7edc\u4f20\u8f93\u6570\u636e\u7684\u5e94\u7528\u573a\u666f\uff0c\u5982\u5ba2\u6237\u7aef-\u670d\u52a1\u5668\u67b6\u6784\u7684\u5e94\u7528\u3002\u901a\u8fc7socket\u7b49\u6a21\u5757\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7f51\u7edc\u6570\u636e\u4f20\u8f93\uff0c\u5904\u7406\u5305\u542b\u7a7a\u683c\u7684\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u591a\u79cd\u5728Python\u4e2d\u5b9e\u73b0\u8f93\u5165\u7a7a\u683c\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528input()\u51fd\u6570\u3001sys.stdin.read()\u51fd\u6570\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001GUI\u6846\u67b6\u3001getpass\u6a21\u5757\u3001\u547d\u4ee4\u884c\u53c2\u6570\u3001\u6587\u4ef6\u8f93\u5165\u3001JSON\u683c\u5f0f\u8f93\u5165\u3001\u73af\u5883\u53d8\u91cf\u3001\u914d\u7f6e\u6587\u4ef6\u3001\u6570\u636e\u5e93\u3001\u7f51\u7edc\u8f93\u5165\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u52bf\uff0c\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u662f\u7b80\u5355\u7684\u547d\u4ee4\u884c\u8f93\u5165\uff0c\u8fd8\u662f\u590d\u6742\u7684\u56fe\u5f62\u7528\u6237\u754c\u9762\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u548c\u5e93\u6765\u5904\u7406\u5404\u79cd\u5f62\u5f0f\u7684\u8f93\u5165\uff0c\u5305\u62ec\u5305\u542b\u7a7a\u683c\u7684\u8f93\u5165\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u7ec4\u5408\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u529f\u80fd\u5f3a\u5927\u3001\u7528\u6237\u53cb\u597d\u7684\u8f93\u5165\u5904\u7406\u673a\u5236\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u63a5\u6536\u7528\u6237\u8f93\u5165\u65f6\u5305\u542b\u7a7a\u683c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u63a5\u6536\u7528\u6237\u8f93\u5165\uff0c\u5305\u62ec\u7a7a\u683c\u3002\u5f53\u7528\u6237\u8f93\u5165\u5b57\u7b26\u4e32\u65f6\uff0c\u6240\u6709\u5b57\u7b26\uff08\u5305\u62ec\u7a7a\u683c\uff09\u90fd\u4f1a\u88ab\u89c6\u4e3a\u5b57\u7b26\u4e32\u7684\u4e00\u90e8\u5206\u3002\u4f8b\u5982\uff0c<code>user_input = input(&quot;\u8bf7\u8f93\u5165\u5185\u5bb9\uff1a&quot;)<\/code>\u3002\u5982\u679c\u7528\u6237\u8f93\u5165\u201cHello World\u201d\uff0c<code>user_input<\/code>\u5c06\u4f1a\u662f\u201cHello World\u201d\uff0c\u5305\u542b\u7a7a\u683c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u683c\uff1f<\/strong><br \/>\u5904\u7406\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u683c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u3002\u53ef\u4ee5\u4f7f\u7528<code>strip()<\/code>\u65b9\u6cd5\u53bb\u9664\u5b57\u7b26\u4e32\u4e24\u7aef\u7684\u7a7a\u683c\uff0c\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u6309\u7a7a\u683c\u5206\u5272\u6210\u5217\u8868\uff0c\u6216\u8005\u4f7f\u7528<code>replace()<\/code>\u65b9\u6cd5\u66ff\u6362\u7a7a\u683c\u3002\u4f8b\u5982\uff0c<code>user_input.replace(&quot; &quot;, &quot;_&quot;)<\/code>\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u683c\u66ff\u6362\u4e3a\u4e0b\u5212\u7ebf\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u7a7a\u683c\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u6216<code>isspace()<\/code>\u65b9\u6cd5\u6765\u68c0\u67e5\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5305\u542b\u7a7a\u683c\u3002\u4f7f\u7528<code>if &quot; &quot; in user_input:<\/code>\u53ef\u4ee5\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u542b\u6709\u7a7a\u683c\uff0c\u800c<code>if user_input.isspace():<\/code>\u5219\u53ef\u4ee5\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u4ec5\u7531\u7a7a\u683c\u7ec4\u6210\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u548c\u5224\u65ad\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u683c\u60c5\u51b5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b9e\u73b0\u8f93\u5165\u7a7a\u683c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528input()\u51fd\u6570\u3001\u4f7f\u7528sys.stdin.rea [&hellip;]","protected":false},"author":3,"featured_media":1153155,"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\/1153150"}],"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=1153150"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1153150\/revisions"}],"predecessor-version":[{"id":1153156,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1153150\/revisions\/1153156"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1153155"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1153150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1153150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1153150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}