{"id":1186212,"date":"2025-01-15T19:49:55","date_gmt":"2025-01-15T11:49:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1186212.html"},"modified":"2025-01-15T19:49:55","modified_gmt":"2025-01-15T11:49:55","slug":"python%e5%a6%82%e4%bd%95%e7%9c%8b%e6%9c%89%e6%b2%a1%e6%9c%89%e6%8d%a2%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1186212.html","title":{"rendered":"python\u5982\u4f55\u770b\u6709\u6ca1\u6709\u6362\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25135107\/3b327e3e-1d87-43af-82e6-7dbf9f5096d0.webp\" alt=\"python\u5982\u4f55\u770b\u6709\u6ca1\u6709\u6362\u884c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u68c0\u67e5\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5305\u542b\u6362\u884c\u7b26\u3001\u4f7f\u7528 <code>\\n<\/code>\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u7b49\u3002<\/strong>\u5176\u4e2d\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5b57\u7b26\u4e32\u7684 <code>find<\/code> \u65b9\u6cd5\u6765\u67e5\u627e\u6362\u884c\u7b26\u7684\u4f4d\u7f6e\u3002<strong>\u4f8b\u5982\uff0c\u4f7f\u7528 <code>s.find(&#39;\\n&#39;)<\/code> \u65b9\u6cd5\uff0c\u5982\u679c\u8fd4\u56de\u503c\u4e0d\u4e3a -1\uff0c\u5219\u8868\u793a\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u6362\u884c\u7b26\u3002<\/strong>\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u901a\u8fc7\u67e5\u627e\u6362\u884c\u7b26\u6765\u68c0\u67e5<\/h3>\n<\/p>\n<p><p>\u67e5\u627e\u6362\u884c\u7b26\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u5185\u7f6e\u65b9\u6cd5 <code>find<\/code>\u3001<code>index<\/code> \u6216\u8005 <code>in<\/code> \u8fd0\u7b97\u7b26\u6765\u68c0\u67e5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528 <code>find<\/code> \u65b9\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">s = &quot;Hello\\nWorld&quot;<\/p>\n<p>if s.find(&#39;\\n&#39;) != -1:<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u6362\u884c\u7b26&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u4e2d\u4e0d\u5305\u542b\u6362\u884c\u7b26&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>find<\/code> \u65b9\u6cd5\u8fd4\u56de\u6362\u884c\u7b26 <code>\\n<\/code> \u5728\u5b57\u7b26\u4e32\u4e2d\u7684\u4f4d\u7f6e\uff0c\u5982\u679c\u8fd4\u56de\u503c\u4e0d\u4e3a -1\uff0c\u5219\u8868\u793a\u5b58\u5728\u6362\u884c\u7b26\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528 <code>index<\/code> \u65b9\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">s = &quot;Hello\\nWorld&quot;<\/p>\n<p>try:<\/p>\n<p>    s.index(&#39;\\n&#39;)<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u6362\u884c\u7b26&quot;)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u4e2d\u4e0d\u5305\u542b\u6362\u884c\u7b26&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>index<\/code> \u65b9\u6cd5\u4e0e <code>find<\/code> \u7c7b\u4f3c\uff0c\u4f46\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u4e0d\u5305\u542b\u6362\u884c\u7b26\uff0c\u5b83\u4f1a\u629b\u51fa\u4e00\u4e2a <code>ValueError<\/code> \u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u4f7f\u7528 <code>in<\/code> \u8fd0\u7b97\u7b26<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">s = &quot;Hello\\nWorld&quot;<\/p>\n<p>if &#39;\\n&#39; in s:<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u6362\u884c\u7b26&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u4e2d\u4e0d\u5305\u542b\u6362\u884c\u7b26&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>in<\/code> \u8fd0\u7b97\u7b26\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u5b83\u76f4\u63a5\u68c0\u67e5\u6362\u884c\u7b26\u662f\u5426\u5728\u5b57\u7b26\u4e32\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u68c0\u67e5<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5339\u914d\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u6765\u68c0\u67e5\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5305\u542b\u6362\u884c\u7b26\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528 <code>re<\/code> \u6a21\u5757<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>s = &quot;Hello\\nWorld&quot;<\/p>\n<p>if re.search(r&#39;\\n&#39;, s):<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u6362\u884c\u7b26&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u4e2d\u4e0d\u5305\u542b\u6362\u884c\u7b26&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>re.search<\/code> \u65b9\u6cd5\u7528\u4e8e\u67e5\u627e\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5305\u542b\u6362\u884c\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 <code>splitlines<\/code><\/h3>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570 <code>splitlines<\/code>\uff0c\u5b83\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u6309\u6362\u884c\u7b26\u62c6\u5206\u6210\u591a\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">s = &quot;Hello\\nWorld&quot;<\/p>\n<p>if len(s.splitlines()) &gt; 1:<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u6362\u884c\u7b26&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u4e2d\u4e0d\u5305\u542b\u6362\u884c\u7b26&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>splitlines<\/code> \u65b9\u6cd5\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\uff0c\u5982\u679c\u5217\u8868\u7684\u957f\u5ea6\u5927\u4e8e 1\uff0c\u5219\u8868\u793a\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u6362\u884c\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u591a\u79cd\u6362\u884c\u7b26<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u5e38\u89c1\u7684 <code>\\n<\/code>\uff0c\u8fd8\u6709\u5176\u4ed6\u5f62\u5f0f\u7684\u6362\u884c\u7b26\uff0c\u5982 <code>\\r<\/code> \u548c <code>\\r\\n<\/code>\u3002\u6211\u4eec\u53ef\u4ee5\u6269\u5c55\u68c0\u67e5\u6765\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5904\u7406\u591a\u79cd\u6362\u884c\u7b26<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">s = &quot;Hello\\r\\nWorld&quot;<\/p>\n<p>if &#39;\\n&#39; in s or &#39;\\r&#39; in s:<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u6362\u884c\u7b26&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u4e2d\u4e0d\u5305\u542b\u6362\u884c\u7b26&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u68c0\u67e5\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5305\u542b <code>\\n<\/code> \u6216 <code>\\r<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5904\u7406\u591a\u79cd\u6362\u884c\u7b26<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>s = &quot;Hello\\r\\nWorld&quot;<\/p>\n<p>if re.search(r&#39;\\r|\\n&#39;, s):<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u6362\u884c\u7b26&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u4e2d\u4e0d\u5305\u542b\u6362\u884c\u7b26&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u4efb\u610f\u5f62\u5f0f\u7684\u6362\u884c\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7efc\u5408\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u7efc\u5408\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u68c0\u67e5\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5305\u542b\u4efb\u4f55\u5f62\u5f0f\u7684\u6362\u884c\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def cont<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ns_newline(s):<\/p>\n<p>    return bool(re.search(r&#39;\\r|\\n&#39;, s))<\/p>\n<p>s = &quot;Hello\\r\\nWorld&quot;<\/p>\n<p>if contains_newline(s):<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u6362\u884c\u7b26&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u4e2d\u4e0d\u5305\u542b\u6362\u884c\u7b26&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a <code>contains_newline<\/code> \u51fd\u6570\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u68c0\u67e5\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5305\u542b\u6362\u884c\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u6362\u884c\u7b26\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u76f4\u63a5\u67e5\u627e\u6362\u884c\u7b26\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u7b49\u3002\u5177\u4f53\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u548c\u5b57\u7b26\u4e32\u7684\u590d\u6742\u7a0b\u5ea6\u6765\u9009\u62e9\u3002<strong>\u76f4\u63a5\u67e5\u627e\u6362\u884c\u7b26\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u5219\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u5339\u914d\u529f\u80fd\uff0c\u800c\u5185\u7f6e\u51fd\u6570 <code>splitlines<\/code> \u5219\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\u3002<\/strong>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u591a\u79cd\u68c0\u67e5\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5305\u542b\u6362\u884c\u7b26\u7684\u65b9\u6cd5\uff0c\u5e76\u80fd\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u7075\u6d3b\u8fd0\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u6362\u884c\u7b26\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u6765\u68c0\u67e5\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5b58\u5728\u6362\u884c\u7b26\u3002\u6362\u884c\u7b26\u5728Python\u4e2d\u901a\u5e38\u8868\u793a\u4e3a<code>\\n<\/code>\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">text = &quot;\u8fd9\u662f\u7b2c\u4e00\u884c\\n\u8fd9\u662f\u7b2c\u4e8c\u884c&quot;\nif &#39;\\n&#39; in text:\n    print(&quot;\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u6362\u884c\u7b26\u3002&quot;)\nelse:\n    print(&quot;\u5b57\u7b26\u4e32\u4e2d\u4e0d\u5305\u542b\u6362\u884c\u7b26\u3002&quot;)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5728Python\u4e2d\u67e5\u627e\u6362\u884c\u7b26\uff1f<\/strong><br \/>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u5904\u7406\u5b57\u7b26\u4e32\u7684\u5f3a\u5927\u5de5\u5177\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u6765\u67e5\u627e\u6362\u884c\u7b26\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import re\ntext = &quot;\u8fd9\u662f\u7b2c\u4e00\u884c\\n\u8fd9\u662f\u7b2c\u4e8c\u884c&quot;\nif re.search(r&#39;\\n&#39;, text):\n    print(&quot;\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u6362\u884c\u7b26\u3002&quot;)\nelse:\n    print(&quot;\u5b57\u7b26\u4e32\u4e2d\u4e0d\u5305\u542b\u6362\u884c\u7b26\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u5408\u9700\u8981\u540c\u65f6\u68c0\u67e5\u5176\u4ed6\u7279\u6b8a\u5b57\u7b26\u7684\u60c5\u51b5\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728\u6587\u4ef6\u8bfb\u53d6\u65f6\u76f4\u63a5\u68c0\u67e5\u6362\u884c\u7b26\uff1f<\/strong><br \/>\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u9010\u884c\u8bfb\u53d6\u5185\u5bb9\u5e76\u68c0\u67e5\u6bcf\u4e00\u884c\u662f\u5426\u5305\u542b\u6362\u884c\u7b26\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:\n    for line in file:\n        if &#39;\\n&#39; in line:\n            print(&quot;\u6b64\u884c\u5305\u542b\u6362\u884c\u7b26\uff1a&quot;, line)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\u66f4\u597d\u5730\u7ba1\u7406\u6362\u884c\u7b26\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u68c0\u67e5\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5305\u542b\u6362\u884c\u7b26\u3001\u4f7f\u7528 \\n\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u7b49\u3002\u5176\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1186212"}],"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=1186212"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1186212\/revisions"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1186212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1186212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1186212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}