{"id":1119627,"date":"2025-01-08T18:46:29","date_gmt":"2025-01-08T10:46:29","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1119627.html"},"modified":"2025-01-08T18:46:31","modified_gmt":"2025-01-08T10:46:31","slug":"python%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4txt%e7%9a%84%e7%89%b9%e5%ae%9a%e8%a1%8c%e7%89%b9%e5%ae%9a%e5%88%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1119627.html","title":{"rendered":"python\u5982\u4f55\u5220\u9664txt\u7684\u7279\u5b9a\u884c\u7279\u5b9a\u5217"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25082627\/74bfa4b2-9a29-4805-a99f-395a9ac34988.webp\" alt=\"python\u5982\u4f55\u5220\u9664txt\u7684\u7279\u5b9a\u884c\u7279\u5b9a\u5217\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5220\u9664txt\u6587\u4ef6\u4e2d\u7684\u7279\u5b9a\u884c\u548c\u7279\u5b9a\u5217\u3002<\/strong> \u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\u3001\u5217\u8868\u64cd\u4f5c\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u8bb2\u89e3\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u901a\u8fc7\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5230\u5217\u8868\uff0c\u7136\u540e\u5bf9\u5217\u8868\u8fdb\u884c\u64cd\u4f5c\uff0c\u6700\u540e\u5c06\u7ed3\u679c\u5199\u56de\u6587\u4ef6\u3002<\/p>\n<\/p>\n<hr>\n<p><p><strong>\u4e00\u3001\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u8bfb\u53d6txt\u6587\u4ef6\u7684\u5185\u5bb9\u3002\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570<code>open()<\/code>\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u53ef\u4ee5\u9009\u62e9\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def read_file(file_path):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        lines = file.readlines()<\/p>\n<p>    return lines<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u4ee5\u53ea\u8bfb\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528<code>readlines<\/code>\u51fd\u6570\u5c06\u6587\u4ef6\u7684\u6bcf\u4e00\u884c\u8bfb\u53d6\u5230\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<hr>\n<p><p><strong>\u4e8c\u3001\u5220\u9664\u7279\u5b9a\u884c<\/strong><\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5220\u9664\u7279\u5b9a\u7684\u884c\u3002\u5047\u8bbe\u6211\u4eec\u8981\u5220\u9664\u7b2c3\u884c\uff08\u7d22\u5f15\u4e3a2\u7684\u884c\uff09\uff0c\u53ef\u4ee5\u76f4\u63a5\u4ece\u5217\u8868\u4e2d\u5220\u9664\u8be5\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def delete_specific_line(lines, line_number):<\/p>\n<p>    if 0 &lt;= line_number &lt; len(lines):<\/p>\n<p>        del lines[line_number]<\/p>\n<p>    return lines<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u68c0\u67e5\u884c\u53f7\u662f\u5426\u5728\u6709\u6548\u8303\u56f4\u5185\uff0c\u7136\u540e\u4f7f\u7528<code>del<\/code>\u5173\u952e\u5b57\u5220\u9664\u6307\u5b9a\u884c\u3002<\/p>\n<\/p>\n<hr>\n<p><p><strong>\u4e09\u3001\u5220\u9664\u7279\u5b9a\u5217<\/strong><\/p>\n<\/p>\n<p><p>\u5220\u9664\u7279\u5b9a\u5217\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\u3002\u6211\u4eec\u9700\u8981\u9010\u884c\u5904\u7406\u6bcf\u4e00\u884c\uff0c\u5e76\u5220\u9664\u6307\u5b9a\u7684\u5217\u3002\u5047\u8bbe\u6211\u4eec\u8981\u5220\u9664\u7b2c2\u5217\uff08\u7d22\u5f15\u4e3a1\u7684\u5217\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>split<\/code>\u548c<code>join<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def delete_specific_column(lines, column_number, delimiter=&#39; &#39;):<\/p>\n<p>    new_lines = []<\/p>\n<p>    for line in lines:<\/p>\n<p>        columns = line.split(delimiter)<\/p>\n<p>        if 0 &lt;= column_number &lt; len(columns):<\/p>\n<p>            del columns[column_number]<\/p>\n<p>        new_line = delimiter.join(columns)<\/p>\n<p>        new_lines.append(new_line)<\/p>\n<p>    return new_lines<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u7528\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5c06\u6bcf\u4e00\u884c\u5206\u5272\u6210\u5217\uff0c\u7136\u540e\u5220\u9664\u6307\u5b9a\u7684\u5217\uff0c\u6700\u540e\u518d\u5c06\u5217\u5408\u5e76\u6210\u65b0\u7684\u4e00\u884c\u3002<\/p>\n<\/p>\n<hr>\n<p><p><strong>\u56db\u3001\u5199\u56de\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u5904\u7406\u5b8c\u5185\u5bb9\u540e\uff0c\u6211\u4eec\u9700\u8981\u5c06\u7ed3\u679c\u5199\u56de\u6587\u4ef6\u3002\u53ef\u4ee5\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u4ee5\u5199\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>writelines<\/code>\u51fd\u6570\u5c06\u5217\u8868\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def write_file(file_path, lines):<\/p>\n<p>    with open(file_path, &#39;w&#39;) as file:<\/p>\n<p>        file.writelines(lines)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u4ee5\u5199\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528<code>writelines<\/code>\u51fd\u6570\u5c06\u5217\u8868\u5185\u5bb9\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<hr>\n<p><p><strong>\u4e94\u3001\u7efc\u5408\u793a\u4f8b<\/strong><\/p>\n<\/p>\n<p><p>\u5c06\u4e0a\u8ff0\u6b65\u9aa4\u7efc\u5408\u5230\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def process_file(file_path, line_number, column_number, delimiter=&#39; &#39;):<\/p>\n<p>    lines = read_file(file_path)<\/p>\n<p>    lines = delete_specific_line(lines, line_number)<\/p>\n<p>    lines = delete_specific_column(lines, column_number, delimiter)<\/p>\n<p>    write_file(file_path, lines)<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>line_number = 2  # \u5220\u9664\u7b2c3\u884c<\/p>\n<p>column_number = 1  # \u5220\u9664\u7b2c2\u5217<\/p>\n<p>delimiter = &#39;,&#39;  # \u5047\u8bbe\u5217\u4e4b\u95f4\u4ee5\u9017\u53f7\u5206\u9694<\/p>\n<p>process_file(file_path, line_number, column_number, delimiter)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>process_file<\/code>\u51fd\u6570\uff0c\u4f9d\u6b21\u8c03\u7528\u524d\u9762\u5b9a\u4e49\u7684\u51fd\u6570\u6765\u5b8c\u6210\u6587\u4ef6\u7684\u5904\u7406\u3002\u7528\u6237\u53ef\u4ee5\u6839\u636e\u9700\u8981\u4fee\u6539<code>file_path<\/code>\u3001<code>line_number<\/code>\u3001<code>column_number<\/code>\u548c<code>delimiter<\/code>\u7684\u503c\u3002<\/p>\n<\/p>\n<hr>\n<p><p><strong>\u516d\u3001\u5904\u7406\u5927\u578b\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u578b\u6587\u4ef6\uff0c\u9010\u884c\u8bfb\u53d6\u548c\u5904\u7406\u53ef\u80fd\u66f4\u4e3a\u9ad8\u6548\u3002\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u5668\u6765\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u5728\u5185\u5b58\u4e2d\u5904\u7406\u6bcf\u4e00\u884c\u540e\u9010\u884c\u5199\u5165\u65b0\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def process_large_file(input_file_path, output_file_path, line_number, column_number, delimiter=&#39; &#39;):<\/p>\n<p>    with open(input_file_path, &#39;r&#39;) as infile, open(output_file_path, &#39;w&#39;) as outfile:<\/p>\n<p>        for current_line_number, line in enumerate(infile):<\/p>\n<p>            if current_line_number == line_number:<\/p>\n<p>                continue  # \u8df3\u8fc7\u8981\u5220\u9664\u7684\u884c<\/p>\n<p>            columns = line.split(delimiter)<\/p>\n<p>            if 0 &lt;= column_number &lt; len(columns):<\/p>\n<p>                del columns[column_number]<\/p>\n<p>            new_line = delimiter.join(columns)<\/p>\n<p>            outfile.write(new_line + &#39;\\n&#39;)<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>input_file_path = &#39;large_example.txt&#39;<\/p>\n<p>output_file_path = &#39;processed_large_example.txt&#39;<\/p>\n<p>line_number = 2  # \u5220\u9664\u7b2c3\u884c<\/p>\n<p>column_number = 1  # \u5220\u9664\u7b2c2\u5217<\/p>\n<p>delimiter = &#39;,&#39;  # \u5047\u8bbe\u5217\u4e4b\u95f4\u4ee5\u9017\u53f7\u5206\u9694<\/p>\n<p>process_large_file(input_file_path, output_file_path, line_number, column_number, delimiter)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u751f\u6210\u5668\u9010\u884c\u8bfb\u53d6\u8f93\u5165\u6587\u4ef6\uff0c\u5e76\u9010\u884c\u5199\u5165\u8f93\u51fa\u6587\u4ef6\u3002\u8fd9\u6837\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u5927\u578b\u6587\u4ef6\uff0c\u907f\u514d\u5185\u5b58\u5360\u7528\u8fc7\u9ad8\u3002<\/p>\n<\/p>\n<hr>\n<p><p><strong>\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u5982\u4f55\u4f7f\u7528Python\u5220\u9664txt\u6587\u4ef6\u4e2d\u7684\u7279\u5b9a\u884c\u548c\u7279\u5b9a\u5217\u3002\u6211\u4eec\u4ecb\u7ecd\u4e86\u8bfb\u53d6\u6587\u4ef6\u3001\u5220\u9664\u7279\u5b9a\u884c\u3001\u5220\u9664\u7279\u5b9a\u5217\u4ee5\u53ca\u5199\u56de\u6587\u4ef6\u7684\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4e86\u5904\u7406\u5927\u578b\u6587\u4ef6\u7684\u89e3\u51b3\u65b9\u6848\u3002\u638c\u63e1\u8fd9\u4e9b\u6280\u672f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u7075\u6d3b\u5730\u5904\u7406\u548c\u64cd\u4f5c\u6587\u672c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6\u5e76\u4fee\u6539TXT\u6587\u4ef6\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6765\u8bfb\u53d6TXT\u6587\u4ef6\u7684\u5185\u5bb9\u3002\u901a\u8fc7\u5c06\u6587\u4ef6\u5185\u5bb9\u8bfb\u53d6\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u7279\u5b9a\u884c\u8fdb\u884c\u4fee\u6539\u3002\u901a\u5e38\u4f7f\u7528<code>readlines()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u6587\u4ef6\u7684\u6bcf\u4e00\u884c\u5b58\u50a8\u4e3a\u5217\u8868\u4e2d\u7684\u4e00\u4e2a\u5143\u7d20\uff0c\u4ece\u800c\u4fbf\u4e8e\u540e\u7eed\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u53ea\u60f3\u5220\u9664\u7279\u5b9a\u884c\uff0c\u8be5\u5982\u4f55\u5b9e\u73b0\uff1f<\/strong><br \/>\u8981\u5220\u9664\u7279\u5b9a\u884c\uff0c\u53ef\u4ee5\u5148\u5c06\u6587\u4ef6\u7684\u6240\u6709\u884c\u8bfb\u53d6\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u7136\u540e\u4f7f\u7528\u5217\u8868\u7684<code>remove()<\/code>\u65b9\u6cd5\u6216\u901a\u8fc7\u5217\u8868\u89e3\u6790\uff08list comprehension\uff09\u521b\u5efa\u4e00\u4e2a\u65b0\u5217\u8868\uff0c\u5176\u4e2d\u4e0d\u5305\u542b\u9700\u8981\u5220\u9664\u7684\u884c\u3002\u5b8c\u6210\u4fee\u6539\u540e\uff0c\u53ef\u4ee5\u5c06\u65b0\u5217\u8868\u5199\u5165\u56de\u539f\u6587\u4ef6\u6216\u53e6\u5b58\u4e3a\u65b0\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5728\u5220\u9664\u7279\u5b9a\u5217\u65f6\uff0c\u6211\u9700\u8981\u8003\u8651\u54ea\u4e9b\u56e0\u7d20\uff1f<\/strong><br \/>\u5220\u9664\u7279\u5b9a\u5217\u901a\u5e38\u6d89\u53ca\u5230\u5bf9\u6bcf\u4e00\u884c\u8fdb\u884c\u5206\u5272\uff0c\u4f8b\u5982\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5\u5c06\u884c\u5206\u5272\u6210\u591a\u4e2a\u90e8\u5206\u3002\u5220\u9664\u7279\u5b9a\u5217\u540e\uff0c\u53ef\u4ee5\u5c06\u5269\u4f59\u7684\u90e8\u5206\u91cd\u65b0\u7ec4\u5408\u6210\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u5176\u5199\u5165\u6587\u4ef6\u3002\u9700\u8981\u6ce8\u610f\u5904\u7406\u597d\u5206\u9694\u7b26\uff0c\u4ee5\u4fdd\u8bc1\u6587\u4ef6\u683c\u5f0f\u7684\u4e00\u81f4\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u4fdd\u8bc1\u5728\u5220\u9664\u884c\u6216\u5217\u540e\uff0c\u6587\u4ef6\u7684\u683c\u5f0f\u4e0d\u4f1a\u53d7\u5230\u5f71\u54cd\uff1f<\/strong><br \/>\u786e\u4fdd\u5728\u4fee\u6539\u6587\u4ef6\u5185\u5bb9\u65f6\uff0c\u4fdd\u6301\u539f\u6709\u683c\u5f0f\u662f\u4e00\u9879\u91cd\u8981\u4efb\u52a1\u3002\u53ef\u4ee5\u5728\u8fdb\u884c\u5220\u9664\u64cd\u4f5c\u524d\uff0c\u5907\u4efd\u539f\u6587\u4ef6\uff0c\u4ee5\u9632\u6b62\u610f\u5916\u6570\u636e\u4e22\u5931\u3002\u4fee\u6539\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u6253\u5370\u6216\u624b\u52a8\u68c0\u67e5\u6587\u4ef6\u5185\u5bb9\uff0c\u786e\u4fdd\u683c\u5f0f\u672a\u53d7\u5230\u5f71\u54cd\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u9002\u5f53\u7684\u6587\u672c\u5206\u9694\u7b26\u548c\u6362\u884c\u7b26\uff0c\u80fd\u591f\u5e2e\u52a9\u4fdd\u6301\u6587\u4ef6\u7684\u7ed3\u6784\u5b8c\u6574\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5220\u9664txt\u6587\u4ef6\u4e2d\u7684\u7279\u5b9a\u884c\u548c\u7279\u5b9a\u5217\u3002 \u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\u3001\u5217\u8868\u64cd\u4f5c\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1119634,"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\/1119627"}],"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=1119627"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1119627\/revisions"}],"predecessor-version":[{"id":1119635,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1119627\/revisions\/1119635"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1119634"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1119627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1119627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1119627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}