{"id":1145355,"date":"2025-01-08T23:07:30","date_gmt":"2025-01-08T15:07:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1145355.html"},"modified":"2025-01-08T23:07:33","modified_gmt":"2025-01-08T15:07:33","slug":"python%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4txt%e4%b8%ad%e7%9a%84%e4%b8%80%e8%a1%8c%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1145355.html","title":{"rendered":"python\u5982\u4f55\u5220\u9664txt\u4e2d\u7684\u4e00\u884c\u6570\u636e\u5e93"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182000\/347548d5-d415-4dfc-836d-5edcd09c377c.webp\" alt=\"python\u5982\u4f55\u5220\u9664txt\u4e2d\u7684\u4e00\u884c\u6570\u636e\u5e93\" \/><\/p>\n<p><p> <strong>Python\u5982\u4f55\u5220\u9664txt\u4e2d\u7684\u4e00\u884c\u6570\u636e\u5e93<\/strong><\/p>\n<\/p>\n<p><p><strong>\u8981\u5220\u9664txt\u6587\u4ef6\u4e2d\u7684\u4e00\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u6587\u4ef6\u5904\u7406\u529f\u80fd\u3001\u4f7f\u7528\u4e34\u65f6\u6587\u4ef6\u4fdd\u5b58\u4fee\u6539\u540e\u7684\u5185\u5bb9\u3001\u5c06\u4fee\u6539\u540e\u7684\u5185\u5bb9\u5199\u56de\u539f\u6587\u4ef6<\/strong>\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><p>\u5220\u9664txt\u6587\u4ef6\u4e2d\u7684\u4e00\u884c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a\u9996\u5148\u8bfb\u53d6\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u627e\u5230\u9700\u8981\u5220\u9664\u7684\u884c\uff0c\u5c06\u5176\u4ed6\u884c\u4fdd\u5b58\u5230\u4e34\u65f6\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u5c06\u4e34\u65f6\u6587\u4ef6\u7684\u5185\u5bb9\u5199\u56de\u5230\u539f\u6587\u4ef6\u4e2d\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u7b80\u5355\u4e14\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u6587\u672c\u6587\u4ef6\u5904\u7406\u9700\u6c42\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open<\/code>\u51fd\u6570\u3002<code>open<\/code>\u51fd\u6570\u53ef\u4ee5\u4ee5\u4e0d\u540c\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u5982\u53ea\u8bfb\u6a21\u5f0f(&#39;r&#39;)\u3001\u5199\u5165\u6a21\u5f0f(&#39;w&#39;)\u3001\u8ffd\u52a0\u6a21\u5f0f(&#39;a&#39;)\u7b49\u3002\u8981\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u53ea\u8bfb\u6a21\u5f0f\uff0c\u5e76\u901a\u8fc7\u8fed\u4ee3\u5904\u7406\u6bcf\u4e00\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    lines = file.readlines()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u6587\u4ef6<code>example.txt<\/code>\u4e2d\u7684\u6240\u6709\u884c\u8bfb\u53d6\u5230\u4e00\u4e2a\u5217\u8868<code>lines<\/code>\u4e2d\uff0c\u6bcf\u4e2a\u5143\u7d20\u5bf9\u5e94\u6587\u4ef6\u4e2d\u7684\u4e00\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u627e\u5230\u5e76\u5220\u9664\u76ee\u6807\u884c<\/h3>\n<\/p>\n<p><p>\u8981\u5220\u9664\u7279\u5b9a\u7684\u884c\uff0c\u53ef\u4ee5\u6839\u636e\u5176\u5185\u5bb9\u6216\u884c\u53f7\u8fdb\u884c\u5339\u914d\u3002\u5047\u8bbe\u6211\u4eec\u8981\u5220\u9664\u5305\u542b\u7279\u5b9a\u5b57\u7b26\u4e32\u7684\u884c\uff0c\u53ef\u4ee5\u8fd9\u6837\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">target_string = &quot;delete me&quot;<\/p>\n<p>lines = [line for line in lines if target_string not in line]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u8981\u5220\u9664\u7279\u5b9a\u884c\u53f7\u7684\u884c\uff0c\u4f8b\u5982\u7b2c3\u884c\uff08\u7d22\u5f15\u4ece0\u5f00\u59cb\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">line_number = 2  # \u8981\u5220\u9664\u7684\u884c\u53f7\uff08\u4ece0\u5f00\u59cb\uff09<\/p>\n<p>lines.pop(line_number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5c06\u4fee\u6539\u540e\u7684\u5185\u5bb9\u5199\u5165\u4e34\u65f6\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u907f\u514d\u6570\u636e\u4e22\u5931\u6216\u6587\u4ef6\u635f\u574f\uff0c\u901a\u5e38\u5148\u5c06\u4fee\u6539\u540e\u7684\u5185\u5bb9\u5199\u5165\u4e00\u4e2a\u4e34\u65f6\u6587\u4ef6\uff0c\u7136\u540e\u518d\u8986\u76d6\u539f\u6587\u4ef6\u3002\u53ef\u4ee5\u4f7f\u7528<code>tempfile<\/code>\u6a21\u5757\u521b\u5efa\u4e34\u65f6\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tempfile<\/p>\n<p>with tempfile.NamedTemporaryFile(delete=False) as temp_file:<\/p>\n<p>    temp_file.writelines(lines)<\/p>\n<p>    temp_file_name = temp_file.name<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5c06\u4e34\u65f6\u6587\u4ef6\u5185\u5bb9\u5199\u56de\u539f\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u6700\u540e\u4e00\u6b65\u662f\u5c06\u4e34\u65f6\u6587\u4ef6\u7684\u5185\u5bb9\u5199\u56de\u5230\u539f\u6587\u4ef6\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528<code>shutil<\/code>\u6a21\u5757\u6765\u66ff\u6362\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>shutil.move(temp_file_name, &#39;example.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5b8c\u6574\u4ee3\u7801\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5220\u9664txt\u6587\u4ef6\u4e2d\u7684\u4e00\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tempfile<\/p>\n<p>import shutil<\/p>\n<p>def delete_line_from_file(file_path, target_string=None, line_number=None):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        lines = file.readlines()<\/p>\n<p>    if target_string:<\/p>\n<p>        lines = [line for line in lines if target_string not in line]<\/p>\n<p>    elif line_number is not None:<\/p>\n<p>        lines.pop(line_number)<\/p>\n<p>    with tempfile.NamedTemporaryFile(delete=False, mode=&#39;w&#39;) as temp_file:<\/p>\n<p>        temp_file.writelines(lines)<\/p>\n<p>        temp_file_name = temp_file.name<\/p>\n<p>    shutil.move(temp_file_name, file_path)<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>delete_line_from_file(&#39;example.txt&#39;, target_string=&quot;delete me&quot;)<\/p>\n<h2><strong>\u6216\u8005<\/strong><\/h2>\n<p>delete_line_from_file(&#39;example.txt&#39;, line_number=2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u5927\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u6587\u4ef6\uff0c\u9010\u884c\u8bfb\u53d6\u548c\u5904\u7406\u53ef\u80fd\u66f4\u4e3a\u9ad8\u6548\u3002\u4ee5\u4e0b\u662f\u9010\u884c\u5904\u7406\u5927\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tempfile<\/p>\n<p>import shutil<\/p>\n<p>def delete_line_from_large_file(file_path, target_string=None, line_number=None):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file, tempfile.NamedTemporaryFile(delete=False, mode=&#39;w&#39;) as temp_file:<\/p>\n<p>        for current_line_number, line in enumerate(file):<\/p>\n<p>            if target_string and target_string in line:<\/p>\n<p>                continue<\/p>\n<p>            if line_number is not None and current_line_number == line_number:<\/p>\n<p>                continue<\/p>\n<p>            temp_file.write(line)<\/p>\n<p>        temp_file_name = temp_file.name<\/p>\n<p>    shutil.move(temp_file_name, file_path)<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>delete_line_from_large_file(&#39;example.txt&#39;, target_string=&quot;delete me&quot;)<\/p>\n<h2><strong>\u6216\u8005<\/strong><\/h2>\n<p>delete_line_from_large_file(&#39;example.txt&#39;, line_number=2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u9ad8\u6548\u5730\u5220\u9664txt\u6587\u4ef6\u4e2d\u7684\u4e00\u884c\u3002\u5173\u952e\u6b65\u9aa4\u5305\u62ec<strong>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3001\u627e\u5230\u5e76\u5220\u9664\u76ee\u6807\u884c\u3001\u5c06\u4fee\u6539\u540e\u7684\u5185\u5bb9\u5199\u5165\u4e34\u65f6\u6587\u4ef6\u3001\u5c06\u4e34\u65f6\u6587\u4ef6\u5185\u5bb9\u5199\u56de\u539f\u6587\u4ef6<\/strong>\u3002\u6b64\u5916\uff0c\u5bf9\u4e8e\u5927\u6587\u4ef6\uff0c\u9010\u884c\u5904\u7406\u53ef\u4ee5\u8282\u7701\u5185\u5b58\u5e76\u63d0\u9ad8\u6548\u7387\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5220\u9664txt\u6587\u4ef6\u4e2d\u7684\u7279\u5b9a\u884c\uff1f<\/strong><br \/>\u8981\u5220\u9664txt\u6587\u4ef6\u4e2d\u7684\u7279\u5b9a\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\u3002\u53ef\u4ee5\u5148\u8bfb\u53d6\u6587\u4ef6\u7684\u6240\u6709\u884c\uff0c\u5c06\u4e0d\u9700\u8981\u5220\u9664\u7684\u884c\u5199\u5165\u4e00\u4e2a\u65b0\u6587\u4ef6\uff0c\u6700\u540e\u7528\u65b0\u6587\u4ef6\u66ff\u6362\u539f\u6587\u4ef6\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;file.txt&#39;, &#39;r&#39;) as file:\n    lines = file.readlines()\n\nwith open(&#39;file.txt&#39;, &#39;w&#39;) as file:\n    for line in lines:\n        if line.strip(&quot;\\n&quot;) != &quot;\u8981\u5220\u9664\u7684\u884c\u5185\u5bb9&quot;:\n            file.write(line)\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528Python\u5220\u9664txt\u6587\u4ef6\u4e2d\u7684\u7a7a\u884c\u6709\u4ec0\u4e48\u529e\u6cd5\u5417\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u7b80\u5355\u7684\u6587\u4ef6\u64cd\u4f5c\u6765\u5b9e\u73b0\u5220\u9664\u7a7a\u884c\u7684\u529f\u80fd\u3002\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u540e\uff0c\u68c0\u67e5\u6bcf\u4e00\u884c\uff0c\u5982\u679c\u4e0d\u662f\u7a7a\u884c\uff0c\u5c31\u5199\u5165\u65b0\u6587\u4ef6\u4e2d\u3002\u4ee5\u4e0b\u662f\u5b9e\u73b0\u7684\u4ee3\u7801\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;file.txt&#39;, &#39;r&#39;) as file:\n    lines = file.readlines()\n\nwith open(&#39;file.txt&#39;, &#39;w&#39;) as file:\n    for line in lines:\n        if line.strip():  # \u68c0\u67e5\u884c\u662f\u5426\u4e3a\u7a7a\n            file.write(line)\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u5927\u6587\u4ef6\u4ee5\u5220\u9664\u7279\u5b9a\u884c\uff1f<\/strong><br \/>\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u907f\u514d\u4e00\u6b21\u6027\u8bfb\u53d6\u6240\u6709\u884c\uff0c\u4f7f\u7528\u9010\u884c\u8bfb\u53d6\u7684\u65b9\u6cd5\u6765\u63d0\u9ad8\u6548\u7387\u3002\u4ee5\u4e0b\u662f\u76f8\u5e94\u7684\u5b9e\u73b0\u65b9\u5f0f\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;file.txt&#39;, &#39;r&#39;) as infile, open(&#39;new_file.txt&#39;, &#39;w&#39;) as outfile:\n    for line in infile:\n        if line.strip(&quot;\\n&quot;) != &quot;\u8981\u5220\u9664\u7684\u884c\u5185\u5bb9&quot;:\n            outfile.write(line)\n<\/code><\/pre>\n<p>\u6267\u884c\u5b8c\u540e\uff0c\u53ef\u4ee5\u5c06<code>new_file.txt<\/code>\u91cd\u547d\u540d\u4e3a\u539f\u6587\u4ef6\u540d\uff0c\u5b8c\u6210\u5220\u9664\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u5220\u9664txt\u4e2d\u7684\u4e00\u884c\u6570\u636e\u5e93 \u8981\u5220\u9664txt\u6587\u4ef6\u4e2d\u7684\u4e00\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u6587\u4ef6\u5904\u7406\u529f\u80fd\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1145363,"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\/1145355"}],"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=1145355"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1145355\/revisions"}],"predecessor-version":[{"id":1145365,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1145355\/revisions\/1145365"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1145363"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1145355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1145355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1145355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}