{"id":1084556,"date":"2025-01-08T13:08:25","date_gmt":"2025-01-08T05:08:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1084556.html"},"modified":"2025-01-08T13:08:28","modified_gmt":"2025-01-08T05:08:28","slug":"python%e5%a6%82%e4%bd%95%e6%8f%90%e5%8f%96%e5%87%batxt%e9%87%8c%e7%9a%84%e5%ad%97%e7%ac%a6-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1084556.html","title":{"rendered":"python\u5982\u4f55\u63d0\u53d6\u51fatxt\u91cc\u7684\u5b57\u7b26"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24194846\/ff69f8a1-7f5d-431e-b6bf-16dab47f7ea2.webp\" alt=\"python\u5982\u4f55\u63d0\u53d6\u51fatxt\u91cc\u7684\u5b57\u7b26\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u63d0\u53d6txt\u6587\u4ef6\u91cc\u7684\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u4f7f\u7528\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\u6765\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u7136\u540e\u4f7f\u7528\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\u5bf9\u8bfb\u53d6\u5230\u7684\u5185\u5bb9\u8fdb\u884c\u64cd\u4f5c\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\u8bfb\u53d6txt\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u7684\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u6765\u6253\u5f00\u6587\u4ef6\uff0c\u4f7f\u7528<code>read<\/code>\u3001<code>readline<\/code>\u6216\u8005<code>readlines<\/code>\u51fd\u6570\u6765\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u4f7f\u7528<code>read<\/code>\u51fd\u6570\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/h4>\n<\/p>\n<p><p><code>read<\/code>\u51fd\u6570\u4e00\u6b21\u6027\u8bfb\u53d6\u6587\u4ef6\u7684\u5168\u90e8\u5185\u5bb9\uff0c\u9002\u7528\u4e8e\u6587\u4ef6\u5185\u5bb9\u8f83\u5c0f\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u6587\u4ef6<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    # \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>open<\/code>\u51fd\u6570\u4ee5\u53ea\u8bfb\u6a21\u5f0f(<code>&#39;r&#39;<\/code>)\u6253\u5f00\u6587\u4ef6\uff0c<code>with<\/code>\u8bed\u53e5\u4f1a\u5728\u6587\u4ef6\u64cd\u4f5c\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\u6587\u4ef6\uff0c<code>read<\/code>\u51fd\u6570\u8bfb\u53d6\u6587\u4ef6\u7684\u5168\u90e8\u5185\u5bb9\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u53d8\u91cf<code>content<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u4f7f\u7528<code>readline<\/code>\u51fd\u6570\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/h4>\n<\/p>\n<p><p><code>readline<\/code>\u51fd\u6570\u6bcf\u6b21\u8bfb\u53d6\u6587\u4ef6\u7684\u4e00\u884c\u5185\u5bb9\uff0c\u9002\u7528\u4e8e\u9010\u884c\u5904\u7406\u6587\u4ef6\u5185\u5bb9\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u6587\u4ef6<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    # \u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>    line = file.readline()<\/p>\n<p>    while line:<\/p>\n<p>        print(line.strip())  # \u4f7f\u7528strip()\u53bb\u9664\u884c\u672b\u7684\u6362\u884c\u7b26<\/p>\n<p>        line = file.readline()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>readline<\/code>\u51fd\u6570\u6bcf\u6b21\u8bfb\u53d6\u6587\u4ef6\u7684\u4e00\u884c\u5185\u5bb9\uff0c\u76f4\u5230\u8bfb\u53d6\u5230\u6587\u4ef6\u672b\u5c3e\u3002<\/p>\n<\/p>\n<p><h4>1.3 \u4f7f\u7528<code>readlines<\/code>\u51fd\u6570\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5230\u5217\u8868\u4e2d<\/h4>\n<\/p>\n<p><p><code>readlines<\/code>\u51fd\u6570\u4e00\u6b21\u6027\u8bfb\u53d6\u6587\u4ef6\u7684\u6240\u6709\u884c\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u6587\u4ef6<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    # \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5230\u5217\u8868\u4e2d<\/p>\n<p>    lines = file.readlines()<\/p>\n<p>    for line in lines:<\/p>\n<p>        print(line.strip())  # \u4f7f\u7528strip()\u53bb\u9664\u884c\u672b\u7684\u6362\u884c\u7b26<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>readlines<\/code>\u51fd\u6570\u4e00\u6b21\u6027\u8bfb\u53d6\u6587\u4ef6\u7684\u6240\u6709\u884c\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5230\u5217\u8868<code>lines<\/code>\u4e2d\uff0c\u7136\u540e\u904d\u5386\u5217\u8868\u9010\u884c\u5904\u7406\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u7279\u5b9a\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5904\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u5339\u914d\u548c\u63d0\u53d6\u7279\u5b9a\u7684\u5b57\u7b26\u3002Python\u63d0\u4f9b\u4e86<code>re<\/code>\u6a21\u5757\u6765\u652f\u6301\u6b63\u5219\u8868\u8fbe\u5f0f\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u63d0\u53d6\u6240\u6709\u6570\u5b57\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u7684\u793a\u4f8b\u4ee3\u7801\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u6570\u5b57\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u6253\u5f00\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    # \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>    content = file.read()<\/p>\n<p>    # \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6240\u6709\u6570\u5b57\u5b57\u7b26<\/p>\n<p>    numbers = re.findall(r&#39;\\d+&#39;, content)<\/p>\n<p>    print(numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>re.findall<\/code>\u51fd\u6570\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f<code>r&#39;\\d+&#39;<\/code>\u63d0\u53d6\u6587\u4ef6\u5185\u5bb9\u4e2d\u7684\u6240\u6709\u6570\u5b57\u5b57\u7b26\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5230\u5217\u8868<code>numbers<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u63d0\u53d6\u6240\u6709\u5355\u8bcd<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u7684\u793a\u4f8b\u4ee3\u7801\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u5355\u8bcd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u6253\u5f00\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    # \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>    content = file.read()<\/p>\n<p>    # \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6240\u6709\u5355\u8bcd<\/p>\n<p>    words = re.findall(r&#39;\\b\\w+\\b&#39;, content)<\/p>\n<p>    print(words)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>re.findall<\/code>\u51fd\u6570\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f<code>r&#39;\\b\\w+\\b&#39;<\/code>\u63d0\u53d6\u6587\u4ef6\u5185\u5bb9\u4e2d\u7684\u6240\u6709\u5355\u8bcd\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5230\u5217\u8868<code>words<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\u63d0\u53d6\u7279\u5b9a\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>Python\u7684\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u7528\u4e8e\u63d0\u53d6\u7279\u5b9a\u7684\u5b57\u7b26\uff0c\u4f8b\u5982<code>split<\/code>\u3001<code>strip<\/code>\u3001<code>replace<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u63d0\u53d6\u7279\u5b9a\u5206\u9694\u7b26\u4e4b\u95f4\u7684\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u7684\u793a\u4f8b\u4ee3\u7801\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>split<\/code>\u65b9\u6cd5\u63d0\u53d6\u7279\u5b9a\u5206\u9694\u7b26\u4e4b\u95f4\u7684\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u6587\u4ef6<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    # \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>    content = file.read()<\/p>\n<p>    # \u4f7f\u7528split\u65b9\u6cd5\u63d0\u53d6\u7279\u5b9a\u5206\u9694\u7b26\u4e4b\u95f4\u7684\u5b57\u7b26<\/p>\n<p>    parts = content.split(&#39;,&#39;)<\/p>\n<p>    for part in parts:<\/p>\n<p>        print(part.strip())  # \u4f7f\u7528strip()\u53bb\u9664\u4e24\u7aef\u7684\u7a7a\u767d\u5b57\u7b26<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>split<\/code>\u65b9\u6cd5\u4f7f\u7528\u9017\u53f7\u5206\u9694\u7b26\u5c06\u6587\u4ef6\u5185\u5bb9\u5206\u5272\u6210\u591a\u4e2a\u90e8\u5206\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5230\u5217\u8868<code>parts<\/code>\u4e2d\uff0c\u7136\u540e\u904d\u5386\u5217\u8868\u9010\u4e2a\u5904\u7406\u5206\u5272\u540e\u7684\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u53bb\u9664\u5b57\u7b26\u4e32\u4e24\u7aef\u7684\u7a7a\u767d\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u7684\u793a\u4f8b\u4ee3\u7801\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>strip<\/code>\u65b9\u6cd5\u53bb\u9664\u5b57\u7b26\u4e32\u4e24\u7aef\u7684\u7a7a\u767d\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u6587\u4ef6<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    # \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>    content = file.read()<\/p>\n<p>    # \u53bb\u9664\u5b57\u7b26\u4e32\u4e24\u7aef\u7684\u7a7a\u767d\u5b57\u7b26<\/p>\n<p>    stripped_content = content.strip()<\/p>\n<p>    print(stripped_content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>strip<\/code>\u65b9\u6cd5\u53bb\u9664\u4e86\u6587\u4ef6\u5185\u5bb9\u5b57\u7b26\u4e32\u4e24\u7aef\u7684\u7a7a\u767d\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7ed3\u5408\u5b9e\u9645\u5e94\u7528\u63d0\u53d6txt\u6587\u4ef6\u4e2d\u7684\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u63d0\u53d6txt\u6587\u4ef6\u4e2d\u7684\u5b57\u7b26\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7ed3\u5408\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u63d0\u53d6\u6587\u4ef6\u4e2d\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u7684\u793a\u4f8b\u4ee3\u7801\u7ed3\u5408\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6587\u4ef6\u4e2d\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u6253\u5f00\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    # \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>    content = file.read()<\/p>\n<p>    # \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u7535\u5b50\u90ae\u4ef6\u5730\u5740<\/p>\n<p>    em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ls = re.findall(r&#39;[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}&#39;, content)<\/p>\n<p>    print(emails)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>re.findall<\/code>\u51fd\u6570\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u63d0\u53d6\u6587\u4ef6\u5185\u5bb9\u4e2d\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5230\u5217\u8868<code>emails<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>4.2 \u63d0\u53d6\u6587\u4ef6\u4e2d\u7684\u7f51\u5740<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u7684\u793a\u4f8b\u4ee3\u7801\u7ed3\u5408\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6587\u4ef6\u4e2d\u7684\u7f51\u5740\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u6253\u5f00\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    # \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>    content = file.read()<\/p>\n<p>    # \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u7f51\u5740<\/p>\n<p>    urls = re.findall(r&#39;https?:\/\/[^\\s&lt;&gt;&quot;]+|www\\.[^\\s&lt;&gt;&quot;]+&#39;, content)<\/p>\n<p>    print(urls)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>re.findall<\/code>\u51fd\u6570\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u63d0\u53d6\u6587\u4ef6\u5185\u5bb9\u4e2d\u7684\u7f51\u5740\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5230\u5217\u8868<code>urls<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>4.3 \u63d0\u53d6\u6587\u4ef6\u4e2d\u7684\u7535\u8bdd\u53f7\u7801<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u7684\u793a\u4f8b\u4ee3\u7801\u7ed3\u5408\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6587\u4ef6\u4e2d\u7684\u7535\u8bdd\u53f7\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u6253\u5f00\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    # \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>    content = file.read()<\/p>\n<p>    # \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u7535\u8bdd\u53f7\u7801<\/p>\n<p>    phone_numbers = re.findall(r&#39;\\b\\d{3}[-.]?\\d{3}[-.]?\\d{4}\\b&#39;, content)<\/p>\n<p>    print(phone_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>re.findall<\/code>\u51fd\u6570\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u63d0\u53d6\u6587\u4ef6\u5185\u5bb9\u4e2d\u7684\u7535\u8bdd\u53f7\u7801\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5230\u5217\u8868<code>phone_numbers<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5728Python\u4e2d\u63d0\u53d6txt\u6587\u4ef6\u4e2d\u7684\u5b57\u7b26\u7684\u51e0\u79cd\u5e38\u7528\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u751a\u81f3\u53ef\u4ee5\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u5b57\u7b26\u63d0\u53d6\u64cd\u4f5c\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u8bb2\u89e3\uff0c\u80fd\u591f\u5e2e\u52a9\u5927\u5bb6\u66f4\u597d\u5730\u638c\u63e1Python\u4e2d\u6587\u4ef6\u64cd\u4f5c\u548c\u5b57\u7b26\u5904\u7406\u7684\u6280\u5de7\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8bfb\u53d6txt\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u4f7f\u7528Python\u8bfb\u53d6txt\u6587\u4ef6\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6765\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>read()<\/code>\u65b9\u6cd5\u8bfb\u53d6\u5176\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;filename.txt&#39;, &#39;r&#39;) as file:\n    content = file.read()\nprint(content)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u6bb5\u5c06\u6253\u5f00\u540d\u4e3a<code>filename.txt<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u5185\u5bb9\u5b58\u50a8\u5728\u53d8\u91cf<code>content<\/code>\u4e2d\u3002<\/p>\n<p><strong>\u5982\u4f55\u4ecetxt\u6587\u4ef6\u4e2d\u63d0\u53d6\u7279\u5b9a\u5b57\u7b26\u6216\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u8981\u4ecetxt\u6587\u4ef6\u4e2d\u63d0\u53d6\u7279\u5b9a\u5b57\u7b26\u6216\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>find()<\/code>\u6216<code>re<\/code>\u6a21\u5757\u8fdb\u884c\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import re\n\nwith open(&#39;filename.txt&#39;, &#39;r&#39;) as file:\n    content = file.read()\n\n# \u63d0\u53d6\u6240\u6709\u6570\u5b57\nnumbers = re.findall(r&#39;\\d+&#39;, content)\nprint(numbers)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u4f1a\u627e\u5230\u5e76\u6253\u5370\u51fa\u6587\u672c\u4e2d\u7684\u6240\u6709\u6570\u5b57\u3002<\/p>\n<p><strong>\u6709\u4ec0\u4e48\u65b9\u6cd5\u53ef\u4ee5\u5904\u7406\u5927\u578btxt\u6587\u4ef6\u4ee5\u907f\u514d\u5185\u5b58\u95ee\u9898\uff1f<\/strong><br \/>\u5904\u7406\u5927\u578btxt\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u52a0\u8f7d\u6240\u6709\u5185\u5bb9\uff0c\u8fd9\u6837\u53ef\u4ee5\u6709\u6548\u8282\u7701\u5185\u5b58\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;largefile.txt&#39;, &#39;r&#39;) as file:\n    for line in file:\n        # \u5728\u8fd9\u91cc\u5904\u7406\u6bcf\u4e00\u884c\n        print(line.strip())\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u4f60\u9010\u884c\u5904\u7406\u6587\u4ef6\uff0c\u9002\u5408\u5904\u7406\u5927\u6587\u4ef6\u800c\u4e0d\u6d88\u8017\u8fc7\u591a\u5185\u5b58\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u63d0\u53d6txt\u6587\u4ef6\u91cc\u7684\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1084569,"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\/1084556"}],"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=1084556"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1084556\/revisions"}],"predecessor-version":[{"id":1084573,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1084556\/revisions\/1084573"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1084569"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1084556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1084556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1084556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}