{"id":959331,"date":"2024-12-27T03:39:46","date_gmt":"2024-12-26T19:39:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/959331.html"},"modified":"2024-12-27T03:39:48","modified_gmt":"2024-12-26T19:39:48","slug":"python%e5%a6%82%e4%bd%95%e6%a3%80%e7%b4%a2txt%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/959331.html","title":{"rendered":"python\u5982\u4f55\u68c0\u7d22txt\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102153\/a54729db-4fd7-4bf9-8864-dd1442be8595.webp\" alt=\"python\u5982\u4f55\u68c0\u7d22txt\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u68c0\u7d22txt\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7279\u5b9a\u6a21\u5f0f\u3001\u9010\u884c\u8bfb\u53d6\u8fdb\u884c\u9010\u884c\u5904\u7406\u3002\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u5229\u7528\u5185\u7f6e\u7684open()\u51fd\u6570\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u7136\u540e\u8fdb\u884c\u5b57\u7b26\u4e32\u67e5\u627e\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u5b9e\u73b0txt\u6587\u4ef6\u7684\u68c0\u7d22\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u8bfb\u53d6\u6587\u4ef6<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u975e\u5e38\u5f3a\u5927\u7684\u5185\u7f6e\u51fd\u6570\u6765\u5904\u7406\u6587\u4ef6\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662fopen()\u51fd\u6570\u3002\u901a\u8fc7open()\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bfb\u53d6txt\u6587\u4ef6\u7684\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6253\u5f00\u6587\u4ef6<\/strong>\uff1a\u9996\u5148\uff0c\u4f7f\u7528open()\u51fd\u6570\u6253\u5f00txt\u6587\u4ef6\u3002\u8be5\u51fd\u6570\u9700\u8981\u4f20\u5165\u4e24\u4e2a\u53c2\u6570\uff1a\u6587\u4ef6\u540d\u548c\u6a21\u5f0f\u3002\u6a21\u5f0f\u53ef\u4ee5\u662f&#39;r&#39;\uff08\u8bfb\u53d6\u6a21\u5f0f\uff09\u3001&#39;w&#39;\uff08\u5199\u5165\u6a21\u5f0f\uff09\u3001&#39;a&#39;\uff08\u8ffd\u52a0\u6a21\u5f0f\uff09\u7b49\u3002\u5bf9\u4e8e\u68c0\u7d22\u6587\u4ef6\u5185\u5bb9\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u8bfb\u53d6\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bfb\u53d6\u5185\u5bb9<\/strong>\uff1a\u6253\u5f00\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528read()\u65b9\u6cd5\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528readline()\u65b9\u6cd5\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u67e5\u627e\u76ee\u6807\u5b57\u7b26\u4e32<\/strong>\uff1a\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\uff0c\u5982find()\u6216in\u5173\u952e\u5b57\uff0c\u6765\u67e5\u627e\u7279\u5b9a\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5173\u95ed\u6587\u4ef6<\/strong>\uff1a\u5728\u5b8c\u6210\u64cd\u4f5c\u540e\uff0c\u52a1\u5fc5\u4f7f\u7528close()\u65b9\u6cd5\u5173\u95ed\u6587\u4ef6\uff0c\u4ee5\u91ca\u653e\u7cfb\u7edf\u8d44\u6e90\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">def search_in_file(file_path, target_string):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        contents = file.read()<\/p>\n<p>        if target_string in contents:<\/p>\n<p>            print(f&quot;&#39;{target_string}&#39; found in file.&quot;)<\/p>\n<p>        else:<\/p>\n<p>            print(f&quot;&#39;{target_string}&#39; not found in file.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u9ad8\u7ea7\u5339\u914d<\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u5904\u7406\u6587\u672c\u65f6\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7279\u522b\u9002\u7528\u4e8e\u590d\u6742\u6a21\u5f0f\u7684\u5339\u914d\u3002Python\u7684re\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5bfc\u5165re\u6a21\u5757<\/strong>\uff1a\u5728\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4e4b\u524d\uff0c\u9700\u8981\u5bfc\u5165re\u6a21\u5757\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\uff1a\u4f7f\u7528re.compile()\u51fd\u6570\u53ef\u4ee5\u5c06\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u7f16\u8bd1\u6210\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61\uff0c\u8fd9\u5bf9\u4e8e\u591a\u6b21\u4f7f\u7528\u540c\u4e00\u6a21\u5f0f\u8fdb\u884c\u5339\u914d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5339\u914d\u5185\u5bb9<\/strong>\uff1a\u4f7f\u7528search()\u3001match()\u3001findall()\u7b49\u65b9\u6cd5\u53ef\u4ee5\u5728\u6587\u672c\u4e2d\u67e5\u627e\u5339\u914d\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def regex_search_in_file(file_path, pattern):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        contents = file.read()<\/p>\n<p>        matches = re.findall(pattern, contents)<\/p>\n<p>        if matches:<\/p>\n<p>            print(f&quot;Matches found: {matches}&quot;)<\/p>\n<p>        else:<\/p>\n<p>            print(&quot;No matches found.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u8fdb\u884c\u5904\u7406<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u53ef\u80fd\u66f4\u5408\u9002\uff0c\u8fd9\u6837\u53ef\u4ee5\u8282\u7701\u5185\u5b58\u5e76\u63d0\u9ad8\u6548\u7387\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u5927\u578b\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9010\u884c\u8bfb\u53d6<\/strong>\uff1a\u4f7f\u7528for\u5faa\u73af\u7ed3\u5408\u6587\u4ef6\u5bf9\u8c61\u53ef\u4ee5\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9010\u884c\u5904\u7406<\/strong>\uff1a\u5728\u6bcf\u4e00\u884c\u4e2d\uff0c\u53ef\u4ee5\u8fdb\u884c\u5b57\u7b26\u4e32\u67e5\u627e\u6216\u6b63\u5219\u5339\u914d\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">def line_by_line_search(file_path, target_string):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        for line_number, line in enumerate(file, start=1):<\/p>\n<p>            if target_string in line:<\/p>\n<p>                print(f&quot;&#39;{target_string}&#39; found on line {line_number}: {line.strip()}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u7b80\u5316\u6587\u4ef6\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>Python\u7684with\u8bed\u53e5\u662f\u4e00\u4e2a\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u7528\u4e8e\u7b80\u5316\u8d44\u6e90\u7684\u83b7\u53d6\u548c\u91ca\u653e\u8fc7\u7a0b\u3002\u5728\u6587\u4ef6\u64cd\u4f5c\u4e2d\uff0c\u4f7f\u7528with\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u4f7f\u7528\u5b8c\u6bd5\u540e\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528with\u8bed\u53e5<\/strong>\uff1a\u901a\u8fc7with open(file_path, &#39;r&#39;) as file:\u8bed\u53e5\u6253\u5f00\u6587\u4ef6\uff0c\u53ef\u4ee5\u5728\u5757\u5185\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\uff0c\u800c\u4e0d\u5fc5\u663e\u5f0f\u8c03\u7528close()\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u81ea\u52a8<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a><\/strong>\uff1a\u5f53with\u8bed\u53e5\u5757\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u6587\u4ef6\u5c06\u81ea\u52a8\u5173\u95ed\uff0c\u786e\u4fdd\u8d44\u6e90\u5f97\u5230\u6b63\u786e\u91ca\u653e\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u7ed3\u5408\u6570\u636e\u7ed3\u6784\u8fdb\u884c\u590d\u6742\u68c0\u7d22<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u590d\u6742\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u7ed3\u5408Python\u7684\u6570\u636e\u7ed3\u6784\uff08\u5982\u5217\u8868\u3001\u5b57\u5178\u7b49\uff09\u8fdb\u884c\u66f4\u590d\u6742\u7684\u68c0\u7d22\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u5217\u8868<\/strong>\uff1a\u5c06\u6bcf\u4e00\u884c\u6216\u5339\u914d\u7684\u7ed3\u679c\u5b58\u50a8\u5728\u5217\u8868\u4e2d\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u6216\u5206\u6790\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u5b57\u5178<\/strong>\uff1a\u5982\u679c\u9700\u8981\u7edf\u8ba1\u7279\u5b9a\u6a21\u5f0f\u7684\u51fa\u73b0\u6b21\u6570\u6216\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u5b58\u50a8\u76f8\u5173\u4fe1\u606f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">def search_and_analyze(file_path, target_string):<\/p>\n<p>    results = []<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        for line_number, line in enumerate(file, start=1):<\/p>\n<p>            if target_string in line:<\/p>\n<p>                results.append((line_number, line.strip()))<\/p>\n<p>    for line_number, content in results:<\/p>\n<p>        print(f&quot;Found on line {line_number}: {content}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5728Python\u4e2d\u68c0\u7d22txt\u6587\u4ef6\u7684\u5185\u5bb9\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u6587\u4ef6\u7684\u68c0\u7d22\u548c\u5206\u6790\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\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\u6253\u5f00\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528<code>read()<\/code>\u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u7684\u6240\u6709\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;file.txt&#39;, &#39;r&#39;) as file:\n    content = file.read()\n    print(content)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u786e\u4fdd\u6587\u4ef6\u5728\u4f7f\u7528\u540e\u88ab\u6b63\u786e\u5173\u95ed\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u6309\u884c\u8bfb\u53d6txt\u6587\u4ef6\uff1f<\/strong><br \/>\u5982\u679c\u5e0c\u671b\u9010\u884c\u5904\u7406\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>readlines()<\/code>\u65b9\u6cd5\u6216\u8fed\u4ee3\u6587\u4ef6\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u4e24\u79cd\u5e38\u7528\u65b9\u6cd5\uff1a  <\/p>\n<pre><code class=\"language-python\"># \u65b9\u6cd51\uff1a\u4f7f\u7528readlines()\nwith open(&#39;file.txt&#39;, &#39;r&#39;) as file:\n    lines = file.readlines()\n    for line in lines:\n        print(line.strip())\n\n# \u65b9\u6cd52\uff1a\u8fed\u4ee3\u6587\u4ef6\u5bf9\u8c61\nwith open(&#39;file.txt&#39;, &#39;r&#39;) as file:\n    for line in file:\n        print(line.strip())\n<\/code><\/pre>\n<p>\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u80fd\u6709\u6548\u5904\u7406\u6587\u4ef6\u4e2d\u7684\u6bcf\u4e00\u884c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u641c\u7d22\u7279\u5b9a\u6587\u672c\u5728txt\u6587\u4ef6\u4e2d\uff1f<\/strong><br \/>\u8981\u5728txt\u6587\u4ef6\u4e2d\u641c\u7d22\u7279\u5b9a\u6587\u672c\uff0c\u53ef\u4ee5\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5e76\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u68c0\u67e5\u6bcf\u4e00\u884c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">search_term = &quot;\u76ee\u6807\u6587\u672c&quot;\nwith open(&#39;file.txt&#39;, &#39;r&#39;) as file:\n    for line_number, line in enumerate(file, start=1):\n        if search_term in line:\n            print(f&quot;\u5728\u7b2c{line_number}\u884c\u627e\u5230\u4e86\uff1a{line.strip()}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5feb\u901f\u5b9a\u4f4d\u6587\u4ef6\u4e2d\u5305\u542b\u7279\u5b9a\u6587\u672c\u7684\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u68c0\u7d22txt\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d [&hellip;]","protected":false},"author":3,"featured_media":959338,"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\/959331"}],"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=959331"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/959331\/revisions"}],"predecessor-version":[{"id":959339,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/959331\/revisions\/959339"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/959338"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=959331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=959331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=959331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}