{"id":993431,"date":"2024-12-27T08:45:59","date_gmt":"2024-12-27T00:45:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/993431.html"},"modified":"2024-12-27T08:46:02","modified_gmt":"2024-12-27T00:46:02","slug":"python%e7%bc%96%e8%be%91%e5%a6%82%e4%bd%95%e5%a4%9a%e8%a1%8c%e8%be%93%e5%85%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/993431.html","title":{"rendered":"python\u7f16\u8f91\u5982\u4f55\u591a\u884c\u8f93\u5165"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070837\/f83f745a-7d47-454f-9dc5-a8df059e668d.webp\" alt=\"python\u7f16\u8f91\u5982\u4f55\u591a\u884c\u8f93\u5165\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8fdb\u884c\u591a\u884c\u8f93\u5165\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u4e09\u5f15\u53f7\u5b57\u7b26\u4e32\u3001\u4f7f\u7528\u8f93\u5165\u51fd\u6570\u7ed3\u5408\u5faa\u73af\u3001\u4ee5\u53ca\u901a\u8fc7\u5217\u8868\u5b58\u50a8\u591a\u884c\u6570\u636e\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u5176\u9002\u7528\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002<\/strong>\u4e0b\u9762\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528\u8f93\u5165\u51fd\u6570\u7ed3\u5408\u5faa\u73af\u8fdb\u884c\u591a\u884c\u8f93\u5165\uff0c\u4ee5\u4fbf\u5728\u4e0d\u540c\u573a\u5408\u4e0b\u5b9e\u73b0\u66f4\u52a0\u7075\u6d3b\u7684\u6570\u636e\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u8f93\u5165\u51fd\u6570\u7ed3\u5408\u5faa\u73af\u662f\u5b9e\u73b0\u591a\u884c\u8f93\u5165\u7684\u5e38\u7528\u65b9\u6cd5\u4e4b\u4e00\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u6838\u5fc3\u601d\u60f3\u662f\u5229\u7528\u4e00\u4e2a\u5faa\u73af\u7ed3\u6784\u4e0d\u65ad\u8c03\u7528\u8f93\u5165\u51fd\u6570\uff0c\u4ee5\u63a5\u53d7\u7528\u6237\u8f93\u5165\u7684\u591a\u884c\u6570\u636e\uff0c\u76f4\u5230\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u4e3a\u6b62\u3002\u901a\u5e38\uff0c\u8fd9\u4e2a\u6761\u4ef6\u662f\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u7279\u5b9a\u7684\u5b57\u7b26\u6216\u5b57\u7b26\u4e32\u6765\u8868\u793a\u8f93\u5165\u7ed3\u675f\u3002\u8fd9\u6837\u4e0d\u4ec5\u80fd\u591f\u7075\u6d3b\u5730\u5b9e\u73b0\u591a\u884c\u8f93\u5165\uff0c\u8fd8\u80fd\u5728\u8f93\u5165\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u6570\u636e\u7684\u5b9e\u65f6\u5904\u7406\u548c\u5b58\u50a8\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4e09\u5f15\u53f7\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><p>\u4e09\u5f15\u53f7\u5b57\u7b26\u4e32\u662fPython\u4e2d\u7528\u4e8e\u5b9a\u4e49\u591a\u884c\u5b57\u7b26\u4e32\u7684\u7b80\u4fbf\u65b9\u6cd5\u3002\u901a\u8fc7\u4f7f\u7528\u4e09\u5f15\u53f7\uff08&#39;&#39;&#39;\u6216&quot;&quot;&quot;\uff09\u5c06\u6587\u672c\u5305\u88f9\u8d77\u6765\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u4ee3\u7801\u4e2d\u8f93\u5165\u591a\u884c\u6587\u5b57\u800c\u65e0\u9700\u4f7f\u7528\u6362\u884c\u7b26\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5728\u4ee3\u7801\u4e2d\u56fa\u5b9a\u7684\u591a\u884c\u6587\u672c\u8f93\u5165\uff0c\u4f8b\u5982\u957f\u6bb5\u6ce8\u91ca\u3001\u6587\u6863\u5b57\u7b26\u4e32\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e09\u5f15\u53f7\u5b9a\u4e49\u591a\u884c\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">multi_line_string = &quot;&quot;&quot;<\/p>\n<p>This is a multi-line string.<\/p>\n<p>It can span multiple lines.<\/p>\n<p>Each line is separated by a newline character.<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>print(multi_line_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u6d01\u76f4\u89c2\uff0c\u9002\u5408\u5728\u4ee3\u7801\u4e2d\u76f4\u63a5\u8f93\u5165\u591a\u884c\u6587\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u7f3a\u70b9<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u4e09\u5f15\u53f7\u5b57\u7b26\u4e32\u7684\u4e3b\u8981\u4f18\u70b9\u662f\u4ee3\u7801\u7b80\u5355\u6613\u8bfb\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u8f93\u5165\u56fa\u5b9a\u591a\u884c\u6587\u672c\u65f6\u975e\u5e38\u65b9\u4fbf\u3002\u4f46\u5176\u7f3a\u70b9\u662f\u65e0\u6cd5\u52a8\u6001\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u591a\u884c\u6570\u636e\uff0c\u56e0\u6b64\u4e0d\u9002\u7528\u4e8e\u9700\u8981\u7528\u6237\u4ea4\u4e92\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u8f93\u5165\u51fd\u6570\u7ed3\u5408\u5faa\u73af<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u8f93\u5165\u51fd\u6570\u7ed3\u5408\u5faa\u73af\u662f\u5b9e\u73b0\u52a8\u6001\u591a\u884c\u8f93\u5165\u7684\u5e38\u7528\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u7528\u6237\u5728\u8fd0\u884c\u65f6\u8f93\u5165\u591a\u884c\u6570\u636e\uff0c\u5e76\u5728\u8f93\u5165\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u6570\u636e\u5904\u7406\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b9e\u73b0\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5faa\u73af\u7ed3\u6784\u6765\u4e0d\u65ad\u8c03\u7528<code>input()<\/code>\u51fd\u6570\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165\u7279\u5b9a\u5b57\u7b26\u8868\u793a\u7ed3\u675f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lines = []<\/p>\n<p>print(&quot;Enter multiple lines of text (type &#39;END&#39; to finish):&quot;)<\/p>\n<p>while True:<\/p>\n<p>    line = input()<\/p>\n<p>    if line == &#39;END&#39;:<\/p>\n<p>        break<\/p>\n<p>    lines.append(line)<\/p>\n<p>print(&quot;\\nYou entered:&quot;)<\/p>\n<p>for line in lines:<\/p>\n<p>    print(line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7528\u6237\u53ef\u4ee5\u8f93\u5165\u591a\u884c\u6587\u672c\uff0c\u76f4\u5230\u8f93\u5165<code>&#39;END&#39;<\/code>\u4e3a\u6b62\u3002\u6240\u6709\u8f93\u5165\u7684\u884c\u5c06\u88ab\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u6700\u540e\u9010\u884c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u7f3a\u70b9<\/strong><\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7075\u6d3b\u6027\u9ad8\uff0c\u9002\u7528\u4e8e\u9700\u8981\u7528\u6237\u4ea4\u4e92\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u5728\u8f93\u5165\u8fc7\u7a0b\u4e2d\u52a8\u6001\u5904\u7406\u6570\u636e\u3002\u7f3a\u70b9\u662f\u9700\u8981\u989d\u5916\u7684\u4ee3\u7801\u6765\u7ba1\u7406\u8f93\u5165\u7ec8\u6b62\u6761\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u901a\u8fc7\u5217\u8868\u5b58\u50a8\u591a\u884c\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u591a\u884c\u8f93\u5165\u65f6\uff0c\u5c06\u6570\u636e\u5b58\u50a8\u5728\u5217\u8868\u4e2d\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u505a\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u7ed3\u5408\u4e09\u5f15\u53f7\u5b57\u7b26\u4e32\u6216\u8f93\u5165\u51fd\u6570\u4f7f\u7528\uff0c\u4ee5\u4fbf\u540e\u7eed\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u573a\u666f<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u9700\u8981\u6279\u91cf\u5904\u7406\u591a\u884c\u6570\u636e\u7684\u573a\u5408\uff0c\u5c06\u6570\u636e\u5b58\u50a8\u5728\u5217\u8868\u4e2d\u662f\u975e\u5e38\u6709\u7528\u7684\u3002\u4f8b\u5982\uff0c\u5904\u7406\u6587\u4ef6\u5185\u5bb9\u3001\u591a\u884c\u7528\u6237\u8f93\u5165\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7ed3\u5408\u8f93\u5165\u51fd\u6570\u548c\u5217\u8868\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = []<\/p>\n<p>print(&quot;Enter data line by line (type &#39;STOP&#39; to finish):&quot;)<\/p>\n<p>while True:<\/p>\n<p>    entry = input()<\/p>\n<p>    if entry == &#39;STOP&#39;:<\/p>\n<p>        break<\/p>\n<p>    data.append(entry)<\/p>\n<h2><strong>Process the collected data<\/strong><\/h2>\n<p>for item in data:<\/p>\n<p>    print(f&quot;Processing: {item}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7528\u6237\u8f93\u5165\u7684\u6bcf\u4e00\u884c\u6570\u636e\u90fd\u4f1a\u88ab\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\uff0c\u8f93\u5165\u7ed3\u675f\u540e\u53ef\u4ee5\u5bf9\u5217\u8868\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u6279\u91cf\u5904\u7406\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5b9e\u73b0\u591a\u884c\u8f93\u5165\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002<strong>\u4f7f\u7528\u4e09\u5f15\u53f7\u5b57\u7b26\u4e32\u9002\u5408\u5904\u7406\u56fa\u5b9a\u7684\u591a\u884c\u6587\u672c\uff0c\u4f7f\u7528\u8f93\u5165\u51fd\u6570\u7ed3\u5408\u5faa\u73af\u9002\u5408\u52a8\u6001\u7528\u6237\u8f93\u5165\uff0c\u800c\u901a\u8fc7\u5217\u8868\u5b58\u50a8\u591a\u884c\u6570\u636e\u5219\u9002\u7528\u4e8e\u9700\u8981\u6279\u91cf\u5904\u7406\u6570\u636e\u7684\u573a\u666f\u3002<\/strong>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u591a\u884c\u8f93\u5165\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u591a\u6b21\u8c03\u7528\u6765\u63a5\u6536\u591a\u884c\u8f93\u5165\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u5faa\u73af\u6765\u5b9e\u73b0\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165\u7279\u5b9a\u7684\u7ec8\u6b62\u6761\u4ef6\u3002\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528<code>sys.stdin.read()<\/code>\u6765\u8bfb\u53d6\u6240\u6709\u8f93\u5165\uff0c\u76f4\u81f3\u7528\u6237\u7ed3\u675f\u8f93\u5165\uff08\u4f8b\u5982\u901a\u8fc7Ctrl+D\uff09\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u6765\u5b58\u50a8\u591a\u884c\u8f93\u5165\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u5c06\u591a\u884c\u8f93\u5165\u5b58\u50a8\u5728\u5217\u8868\u4e2d\u3002\u901a\u8fc7\u5728\u5faa\u73af\u4e2d\u8c03\u7528<code>input()<\/code>\uff0c\u6bcf\u6b21\u8f93\u5165\u540e\u5c06\u6570\u636e\u8ffd\u52a0\u5230\u5217\u8868\u4e2d\u3002\u6700\u540e\uff0c\u60a8\u5c06\u62e5\u6709\u4e00\u4e2a\u5305\u542b\u6240\u6709\u8f93\u5165\u884c\u7684\u5217\u8868\uff0c\u4fbf\u4e8e\u540e\u7eed\u5904\u7406\u6216\u5206\u6790\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u591a\u884c\u8f93\u5165\u4e2d\u7684\u7a7a\u884c\u6216\u7279\u5b9a\u683c\u5f0f\u7684\u8f93\u5165\uff1f<\/strong><br \/>\u5728\u83b7\u53d6\u591a\u884c\u8f93\u5165\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u68c0\u67e5\u8f93\u5165\u662f\u5426\u4e3a\u7a7a\u6216\u7b26\u5408\u67d0\u79cd\u683c\u5f0f\u3002\u60a8\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u6dfb\u52a0\u903b\u8f91\uff0c\u5ffd\u7565\u7a7a\u884c\u6216\u5bf9\u8f93\u5165\u8fdb\u884c\u683c\u5f0f\u9a8c\u8bc1\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u6709\u6548\u6027\u548c\u4e00\u81f4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8fdb\u884c\u591a\u884c\u8f93\u5165\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u4e09\u5f15\u53f7\u5b57\u7b26\u4e32\u3001\u4f7f\u7528\u8f93\u5165\u51fd\u6570\u7ed3\u5408\u5faa\u73af\u3001\u4ee5\u53ca\u901a\u8fc7\u5217\u8868 [&hellip;]","protected":false},"author":3,"featured_media":993443,"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\/993431"}],"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=993431"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/993431\/revisions"}],"predecessor-version":[{"id":993446,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/993431\/revisions\/993446"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/993443"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=993431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=993431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=993431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}