{"id":1080787,"date":"2025-01-08T12:33:26","date_gmt":"2025-01-08T04:33:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1080787.html"},"modified":"2025-01-08T12:33:28","modified_gmt":"2025-01-08T04:33:28","slug":"python%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96%e5%a4%9a%e8%a1%8c%e8%be%93%e5%85%a5%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1080787.html","title":{"rendered":"python\u5982\u4f55\u8bfb\u53d6\u591a\u884c\u8f93\u5165\u6570\u636e\u7c7b\u578b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183135\/03693112-1a1c-4c07-8018-e270006f0f52.webp\" alt=\"python\u5982\u4f55\u8bfb\u53d6\u591a\u884c\u8f93\u5165\u6570\u636e\u7c7b\u578b\" \/><\/p>\n<p><p> <strong>Python\u8bfb\u53d6\u591a\u884c\u8f93\u5165\u6570\u636e\u7c7b\u578b\u7684\u65b9\u6cd5\u4e3b\u8981\u6709<\/strong>\uff1a<code>input()\u51fd\u6570\u8bfb\u53d6\u3001sys.stdin\u8bfb\u53d6\u3001\u6587\u4ef6\u8bfb\u53d6\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u8bfb\u53d6\u3001\u5faa\u73af\u8bfb\u53d6<\/code>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528sys.stdin\u8bfb\u53d6<\/strong>\u662f\u4e00\u79cd\u975e\u5e38\u5e38\u89c1\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u5904\u7406\u8f83\u5927\u7684\u8f93\u5165\u6570\u636e\u91cf\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528input()\u51fd\u6570\u8bfb\u53d6\uff1a<\/p>\n<p>Python\u5185\u7f6e\u7684input()\u51fd\u6570\u662f\u7528\u4e8e\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u6700\u57fa\u672c\u65b9\u6cd5\u3002\u5f53\u9700\u8981\u8bfb\u53d6\u591a\u884c\u8f93\u5165\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u8c03\u7528input()\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u7684\u8f93\u5165\u6570\u636e\u3002\u4f8b\u5982\u8bfb\u53d63\u884c\u8f93\u5165\uff0c\u53ef\u4ee5\u5982\u4e0b\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lines = []<\/p>\n<p>for _ in range(3):<\/p>\n<p>    line = input()<\/p>\n<p>    lines.append(line)<\/p>\n<p>print(lines)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528sys.stdin\u8bfb\u53d6\uff1a<\/p>\n<p>\u5bf9\u4e8e\u5927\u89c4\u6a21\u7684\u8f93\u5165\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528sys.stdin\u8bfb\u53d6\uff0c\u8fd9\u662f\u56e0\u4e3asys.stdin.read()\u53ef\u4ee5\u4e00\u6b21\u6027\u8bfb\u53d6\u6240\u6709\u8f93\u5165\u6570\u636e\uff0c\u76f8\u6bd4\u4e8e\u9010\u884c\u8bfb\u53d6\u7684input()\uff0c\u5728\u5904\u7406\u5927\u6570\u636e\u65f6\u4f1a\u66f4\u9ad8\u6548\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>data = sys.stdin.read()<\/p>\n<p>lines = data.splitlines()<\/p>\n<p>print(lines)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8be6\u7ec6\u63cf\u8ff0\uff1a<strong>sys.stdin\u8bfb\u53d6<\/strong>\uff1a<\/p>\n<p>sys.stdin\u662f\u4e00\u4e2a\u6587\u4ef6\u5bf9\u8c61\uff0c\u53ef\u4ee5\u50cf\u5904\u7406\u6587\u4ef6\u4e00\u6837\u5904\u7406\u8f93\u5165\u3002\u901a\u8fc7sys.stdin.read()\u53ef\u4ee5\u4e00\u6b21\u6027\u8bfb\u53d6\u6240\u6709\u8f93\u5165\u6570\u636e\uff0c\u7136\u540e\u4f7f\u7528splitlines()\u65b9\u6cd5\u5c06\u5176\u5206\u5272\u6210\u591a\u884c\u5217\u8868\u3002\u8fd9\u6837\u7684\u65b9\u6cd5\u5728\u5904\u7406\u5927\u578b\u8f93\u5165\u6570\u636e\u65f6\u6548\u7387\u66f4\u9ad8\uff0c\u4e14\u4ee3\u7801\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4ece\u6587\u4ef6\u8bfb\u53d6\uff1a<\/p>\n<p>\u5f53\u591a\u884c\u8f93\u5165\u6570\u636e\u5b58\u50a8\u5728\u6587\u4ef6\u4e2d\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6587\u4ef6\u8bfb\u53d6\u64cd\u4f5c\u6765\u83b7\u53d6\u6570\u636e\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u5904\u7406\u9884\u5148\u5b58\u50a8\u5728\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;input.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    lines = file.readlines()<\/p>\n<p>print(lines)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8bfb\u53d6\uff1a<\/p>\n<p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u6765\u751f\u6210\u5217\u8868\u3002\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408input()\u51fd\u6570\u548c\u5faa\u73af\u6765\u8bfb\u53d6\u591a\u884c\u8f93\u5165\u6570\u636e\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lines = [input() for _ in range(3)]<\/p>\n<p>print(lines)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528\u5faa\u73af\u8bfb\u53d6\uff1a<\/p>\n<p>\u5bf9\u4e8e\u4e0d\u786e\u5b9a\u884c\u6570\u7684\u8f93\u5165\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u8bfb\u53d6\u76f4\u5230\u8f93\u5165\u7ed3\u675f\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u4ea4\u4e92\u5f0f\u8f93\u5165\u573a\u666f\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lines = []<\/p>\n<p>while True:<\/p>\n<p>    try:<\/p>\n<p>        line = input()<\/p>\n<p>        if line:<\/p>\n<p>            lines.append(line)<\/p>\n<p>        else:<\/p>\n<p>            break<\/p>\n<p>    except EOFError:<\/p>\n<p>        break<\/p>\n<p>print(lines)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5177\u4f53\u5185\u5bb9\u5c55\u5f00<\/h3>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528input()\u51fd\u6570\u8bfb\u53d6<\/p>\n<\/p>\n<p><p>input()\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u4ece\u6807\u51c6\u8f93\u5165\u4e2d\u8bfb\u53d6\u4e00\u884c\u6570\u636e\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cinput()\u4f1a\u5c06\u8f93\u5165\u6570\u636e\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8fd4\u56de\u3002\u5f53\u9700\u8981\u8bfb\u53d6\u591a\u884c\u8f93\u5165\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lines = []<\/p>\n<p>for _ in range(3):<\/p>\n<p>    line = input(&quot;Enter a line: &quot;)<\/p>\n<p>    lines.append(line)<\/p>\n<p>print(&quot;You entered:&quot;)<\/p>\n<p>print(lines)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5faa\u73af3\u6b21\u8c03\u7528input()\u51fd\u6570\uff0c\u8bfb\u53d6\u6bcf\u4e00\u884c\u8f93\u5165\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u5217\u8868lines\u4e2d\u3002\u6700\u540e\uff0c\u8f93\u51fa\u5217\u8868\u4e2d\u7684\u6240\u6709\u884c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528sys.stdin\u8bfb\u53d6<\/p>\n<\/p>\n<p><p>sys.stdin\u662fPython\u4e2d\u7528\u4e8e\u6807\u51c6\u8f93\u5165\u7684\u6587\u4ef6\u5bf9\u8c61\u3002\u76f8\u6bd4\u4e8einput()\u51fd\u6570\uff0csys.stdin.read()\u53ef\u4ee5\u4e00\u6b21\u6027\u8bfb\u53d6\u6240\u6709\u8f93\u5165\u6570\u636e\uff0c\u7136\u540e\u4f7f\u7528splitlines()\u65b9\u6cd5\u5c06\u5176\u5206\u5272\u6210\u591a\u884c\u5217\u8868\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>data = sys.stdin.read()<\/p>\n<p>lines = data.splitlines()<\/p>\n<p>print(&quot;You entered:&quot;)<\/p>\n<p>print(lines)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>sys.stdin.read()\u8bfb\u53d6\u6240\u6709\u8f93\u5165\u6570\u636e\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u8fd4\u56de\u3002\u7136\u540e\uff0csplitlines()\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u6309\u884c\u5206\u5272\uff0c\u751f\u6210\u4e00\u4e2a\u5305\u542b\u591a\u884c\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>sys.stdin\u8bfb\u53d6\u7684\u4f18\u70b9\u5728\u4e8e\u5b83\u7684\u6548\u7387\u66f4\u9ad8\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u5904\u7406\u5927\u91cf\u6570\u636e\u7684\u60c5\u51b5\u3002\u5728\u7ade\u8d5b\u7f16\u7a0b\u6216\u6279\u91cf\u6570\u636e\u5904\u7406\u7684\u573a\u666f\u4e2d\uff0csys.stdin.read()\u975e\u5e38\u5e38\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4ece\u6587\u4ef6\u8bfb\u53d6<\/p>\n<\/p>\n<p><p>\u5982\u679c\u591a\u884c\u8f93\u5165\u6570\u636e\u5b58\u50a8\u5728\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6587\u4ef6\u8bfb\u53d6\u64cd\u4f5c\u6765\u83b7\u53d6\u6570\u636e\u3002Python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;input.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    lines = file.readlines()<\/p>\n<p>print(&quot;File contents:&quot;)<\/p>\n<p>print(lines)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0copen()\u51fd\u6570\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u6587\u4ef6\u5bf9\u8c61\u3002readlines()\u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u884c\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002\u6700\u540e\uff0c\u8f93\u51fa\u5217\u8868\u4e2d\u7684\u6240\u6709\u884c\u3002<\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u5904\u7406\u9884\u5148\u5b58\u50a8\u5728\u6587\u4ef6\u4e2d\u7684\u6570\u636e\uff0c\u4f8b\u5982\u65e5\u5fd7\u6587\u4ef6\u3001\u914d\u7f6e\u6587\u4ef6\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8bfb\u53d6<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\u7ed3\u6784\uff0c\u7528\u4e8e\u751f\u6210\u5217\u8868\u3002\u7ed3\u5408input()\u51fd\u6570\u548c\u5faa\u73af\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u8bfb\u53d6\u591a\u884c\u8f93\u5165\u6570\u636e\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lines = [input(&quot;Enter a line: &quot;) for _ in range(3)]<\/p>\n<p>print(&quot;You entered:&quot;)<\/p>\n<p>print(lines)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e00\u4e2a\u5305\u542b3\u884c\u8f93\u5165\u6570\u636e\u7684\u5217\u8868\u3002\u6bcf\u6b21\u5faa\u73af\u8c03\u7528input()\u51fd\u6570\uff0c\u8bfb\u53d6\u4e00\u884c\u8f93\u5165\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u5217\u8868\u4e2d\u3002\u6700\u540e\uff0c\u8f93\u51fa\u5217\u8868\u4e2d\u7684\u6240\u6709\u884c\u3002<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u4f18\u70b9\u5728\u4e8e\u8bed\u6cd5\u7b80\u6d01\uff0c\u4ee3\u7801\u66f4\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528\u5faa\u73af\u8bfb\u53d6<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e0d\u786e\u5b9a\u884c\u6570\u7684\u8f93\u5165\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u8bfb\u53d6\u76f4\u5230\u8f93\u5165\u7ed3\u675f\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u4ea4\u4e92\u5f0f\u8f93\u5165\u573a\u666f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lines = []<\/p>\n<p>while True:<\/p>\n<p>    try:<\/p>\n<p>        line = input(&quot;Enter a line (or press Enter to finish): &quot;)<\/p>\n<p>        if line:<\/p>\n<p>            lines.append(line)<\/p>\n<p>        else:<\/p>\n<p>            break<\/p>\n<p>    except EOFError:<\/p>\n<p>        break<\/p>\n<p>print(&quot;You entered:&quot;)<\/p>\n<p>print(lines)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5faa\u73af\u8c03\u7528input()\u51fd\u6570\uff0c\u8bfb\u53d6\u6bcf\u4e00\u884c\u8f93\u5165\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u5217\u8868\u4e2d\u3002\u5982\u679c\u7528\u6237\u8f93\u5165\u4e3a\u7a7a\u884c\uff0c\u5219\u9000\u51fa\u5faa\u73af\u3002\u6700\u540e\uff0c\u8f93\u51fa\u5217\u8868\u4e2d\u7684\u6240\u6709\u884c\u3002<\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u786e\u5b9a\u884c\u6570\u7684\u8f93\u5165\u573a\u666f\uff0c\u4f8b\u5982\u4ea4\u4e92\u5f0f\u547d\u4ee4\u884c\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0sys.stdin\u8bfb\u53d6<\/h3>\n<\/p>\n<p><p>sys.stdin\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6587\u4ef6\u5bf9\u8c61\uff0c\u8868\u793a\u6807\u51c6\u8f93\u5165\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u9ad8\u6548\u7684\u65b9\u5f0f\u6765\u8bfb\u53d6\u8f93\u5165\u6570\u636e\uff0c\u7279\u522b\u9002\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u7684\u60c5\u51b5\u3002sys.stdin.read()\u53ef\u4ee5\u4e00\u6b21\u6027\u8bfb\u53d6\u6240\u6709\u8f93\u5165\u6570\u636e\uff0c\u7136\u540e\u4f7f\u7528splitlines()\u65b9\u6cd5\u5c06\u5176\u5206\u5272\u6210\u591a\u884c\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u76f8\u6bd4\u4e8einput()\u51fd\u6570\u9010\u884c\u8bfb\u53d6\u6570\u636e\uff0csys.stdin.read()\u7684\u6548\u7387\u66f4\u9ad8\uff0c\u4ee3\u7801\u4e5f\u66f4\u52a0\u7b80\u6d01\u3002\u5728\u7ade\u8d5b\u7f16\u7a0b\u6216\u6279\u91cf\u6570\u636e\u5904\u7406\u7684\u573a\u666f\u4e2d\uff0csys.stdin.read()\u975e\u5e38\u5e38\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b\u8f93\u5165\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code>line1<\/p>\n<p>line2<\/p>\n<p>line3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528sys.stdin.read()\u8bfb\u53d6\u8fd9\u4e9b\u6570\u636e\u5e76\u5c06\u5176\u5206\u5272\u6210\u591a\u884c\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>data = sys.stdin.read()<\/p>\n<p>lines = data.splitlines()<\/p>\n<p>print(&quot;You entered:&quot;)<\/p>\n<p>print(lines)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0csys.stdin.read()\u8bfb\u53d6\u6240\u6709\u8f93\u5165\u6570\u636e\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u8fd4\u56de\u3002\u7136\u540e\uff0csplitlines()\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u6309\u884c\u5206\u5272\uff0c\u751f\u6210\u4e00\u4e2a\u5305\u542b\u591a\u884c\u7684\u5217\u8868\u3002\u6700\u540e\uff0c\u8f93\u51fa\u5217\u8868\u4e2d\u7684\u6240\u6709\u884c\u3002<\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u9ad8\u6548\u3001\u7b80\u6d01\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u5176\u4ed6\u8bfb\u53d6\u591a\u884c\u8f93\u5165\u6570\u636e\u7c7b\u578b\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u8bfb\u53d6\u591a\u884c\u8f93\u5165\u6570\u636e\u7c7b\u578b\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528sys\u6a21\u5757\u7684sys.stdin.readlines()\u65b9\u6cd5<\/li>\n<li>\u4f7f\u7528itertools\u6a21\u5757\u7684itertools.islice()\u65b9\u6cd5<\/li>\n<li>\u4f7f\u7528fileinput\u6a21\u5757\u7684fileinput.input()\u65b9\u6cd5<\/li>\n<\/ol>\n<p><p>\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528sys.stdin.readlines()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>sys.stdin.readlines()\u65b9\u6cd5\u4e00\u6b21\u6027\u8bfb\u53d6\u6240\u6709\u8f93\u5165\u6570\u636e\uff0c\u5e76\u5c06\u5176\u6309\u884c\u5206\u5272\uff0c\u751f\u6210\u4e00\u4e2a\u5305\u542b\u591a\u884c\u7684\u5217\u8868\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>lines = sys.stdin.readlines()<\/p>\n<p>print(&quot;You entered:&quot;)<\/p>\n<p>print(lines)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>sys.stdin.readlines()\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u7b80\u5355\u6613\u7528\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5c0f\u89c4\u6a21\u6570\u636e\u7684\u573a\u666f\u3002\u4f46\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\uff0csys.stdin.read()\u65b9\u6cd5\u66f4\u4e3a\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528itertools.islice()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>itertools.islice()\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u4ece\u8fed\u4ee3\u5668\u4e2d\u5207\u7247\uff0c\u751f\u6210\u6307\u5b9a\u6570\u91cf\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>import itertools<\/p>\n<p>lines = list(itertools.islice(sys.stdin, 3))<\/p>\n<p>print(&quot;You entered:&quot;)<\/p>\n<p>print(lines)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0citertools.islice()\u65b9\u6cd5\u4ecesys.stdin\u8fed\u4ee3\u5668\u4e2d\u5207\u7247\uff0c\u751f\u62103\u884c\u8f93\u5165\u6570\u636e\u3002\u7136\u540e\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u5e76\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><p>itertools.islice()\u65b9\u6cd5\u9002\u7528\u4e8e\u5904\u7406\u6307\u5b9a\u6570\u91cf\u7684\u8f93\u5165\u6570\u636e\uff0c\u4ee3\u7801\u7b80\u6d01\u4e14\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528fileinput.input()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>fileinput\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u53ef\u4ee5\u7528\u6765\u5904\u7406\u591a\u4e2a\u8f93\u5165\u6587\u4ef6\u7684\u8f93\u5165\u63a5\u53e3\u3002fileinput.input()\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u53ef\u4ee5\u8fed\u4ee3\u7684\u6587\u4ef6\u5bf9\u8c61\uff0c\u7528\u4e8e\u9010\u884c\u8bfb\u53d6\u8f93\u5165\u6570\u636e\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import fileinput<\/p>\n<p>lines = []<\/p>\n<p>for line in fileinput.input():<\/p>\n<p>    lines.append(line)<\/p>\n<p>print(&quot;You entered:&quot;)<\/p>\n<p>print(lines)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cfileinput.input()\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u53ef\u4ee5\u8fed\u4ee3\u7684\u6587\u4ef6\u5bf9\u8c61\uff0c\u9010\u884c\u8bfb\u53d6\u8f93\u5165\u6570\u636e\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u5217\u8868\u4e2d\u3002\u6700\u540e\uff0c\u8f93\u51fa\u5217\u8868\u4e2d\u7684\u6240\u6709\u884c\u3002<\/p>\n<\/p>\n<p><p>fileinput.input()\u65b9\u6cd5\u9002\u7528\u4e8e\u5904\u7406\u591a\u4e2a\u8f93\u5165\u6587\u4ef6\u7684\u573a\u666f\uff0c\u4ee3\u7801\u7b80\u6d01\u4e14\u6613\u4e8e\u6269\u5c55\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u8bfb\u53d6\u591a\u884c\u8f93\u5165\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ecinput()\u51fd\u6570\u8bfb\u53d6\u3001sys.stdin\u8bfb\u53d6\u3001\u6587\u4ef6\u8bfb\u53d6\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u8bfb\u53d6\u3001\u5faa\u73af\u8bfb\u53d6\u7b49\u3002\u5176\u4e2d\uff0c\u4f7f\u7528sys.stdin.read()\u8bfb\u53d6\u662f\u4e00\u79cd\u9ad8\u6548\u4e14\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u7279\u522b\u9002\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002\u5728\u5904\u7406\u8f93\u5165\u6570\u636e\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u8f93\u5165\u6570\u636e\u7684\u683c\u5f0f\u548c\u89c4\u6a21\uff0c\u4ee5\u9009\u62e9\u6700\u4f73\u7684\u8bfb\u53d6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u591a\u884c\u8f93\u5165\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u6765\u8bfb\u53d6\u591a\u884c\u8f93\u5165\u3002\u4e3a\u4e86\u8bfb\u53d6\u591a\u884c\u6570\u636e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5faa\u73af\u6765\u6536\u96c6\u6bcf\u4e00\u884c\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165\u7279\u5b9a\u7684\u7ed3\u675f\u6807\u5fd7\uff08\u4f8b\u5982\uff0c\u7a7a\u884c\u6216\u7279\u5b9a\u5b57\u7b26\uff09\u3002\u5c06\u6bcf\u4e00\u884c\u5b58\u50a8\u5728\u5217\u8868\u4e2d\uff0c\u6700\u540e\u53ef\u4ee5\u5bf9\u8fd9\u4e9b\u884c\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u591a\u884c\u8f93\u5165\u8f6c\u6362\u4e3a\u7279\u5b9a\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>\u5728\u8bfb\u53d6\u591a\u884c\u8f93\u5165\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u5faa\u73af\u5c06\u5176\u8f6c\u6362\u4e3a\u6240\u9700\u7684\u6570\u636e\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u5e0c\u671b\u5c06\u8f93\u5165\u7684\u6bcf\u4e00\u884c\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u53ef\u4ee5\u5728\u6536\u96c6\u8f93\u5165\u65f6\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u5f02\u5e38\u60c5\u51b5\uff1f<\/strong><br \/>\u5904\u7406\u591a\u884c\u8f93\u5165\u65f6\uff0c\u7528\u6237\u53ef\u80fd\u4f1a\u8f93\u5165\u4e0d\u7b26\u5408\u9884\u671f\u7684\u6570\u636e\u683c\u5f0f\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u548c\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u4e0d\u4f1a\u56e0\u4e3a\u65e0\u6548\u8f93\u5165\u800c\u5d29\u6e83\u3002\u53ef\u4ee5\u63d0\u793a\u7528\u6237\u91cd\u65b0\u8f93\u5165\u6709\u6548\u6570\u636e\uff0c\u589e\u5f3a\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bfb\u53d6\u591a\u884c\u8f93\u5165\u6570\u636e\u7c7b\u578b\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1ainput()\u51fd\u6570\u8bfb\u53d6\u3001sys.stdin\u8bfb\u53d6\u3001\u6587\u4ef6\u8bfb\u53d6\u3001\u5217\u8868\u63a8 [&hellip;]","protected":false},"author":3,"featured_media":1080793,"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\/1080787"}],"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=1080787"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1080787\/revisions"}],"predecessor-version":[{"id":1080796,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1080787\/revisions\/1080796"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1080793"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1080787"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1080787"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1080787"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}