{"id":1136858,"date":"2025-01-08T21:40:59","date_gmt":"2025-01-08T13:40:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1136858.html"},"modified":"2025-01-08T21:41:01","modified_gmt":"2025-01-08T13:41:01","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8python%e4%b8%ad%e7%ae%b1%e6%96%87%e6%9c%ac%e6%96%87%e4%bb%b6%e8%be%93%e5%85%a5%e6%95%b4%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1136858.html","title":{"rendered":"\u5982\u4f55\u5728Python\u4e2d\u7bb1\u6587\u672c\u6587\u4ef6\u8f93\u5165\u6574\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100841\/f7ad0c33-2059-449a-a91a-ce54cab3f06c.webp\" alt=\"\u5982\u4f55\u5728Python\u4e2d\u7bb1\u6587\u672c\u6587\u4ef6\u8f93\u5165\u6574\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u7bb1\u6587\u672c\u6587\u4ef6\u8f93\u5165\u6574\u6570\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5173\u952e\u5728\u4e8e\u9009\u62e9\u5408\u9002\u7684\u6587\u4ef6\u64cd\u4f5c\u65b9\u5f0f\u3001\u786e\u4fdd\u6587\u4ef6\u683c\u5f0f\u6b63\u786e\u3001\u5e76\u6b63\u786e\u5904\u7406\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u65b9\u6cd5\uff1a\u6587\u4ef6\u8bfb\u53d6\u3001\u6570\u636e\u8f6c\u6362\u3001\u5f02\u5e38\u5904\u7406\u3001\u6587\u4ef6\u5173\u95ed\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u6587\u4ef6\u8bfb\u53d6<\/strong> \u662f\u6700\u57fa\u7840\u4e14\u5173\u952e\u7684\u4e00\u6b65\uff0c\u901a\u8fc7 <code>open()<\/code> \u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5b9e\u73b0\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u6587\u4ef6\u8bfb\u53d6<\/h2>\n<\/p>\n<p><p>\u6587\u4ef6\u8bfb\u53d6\u662f\u5904\u7406\u6587\u672c\u6587\u4ef6\u7684\u7b2c\u4e00\u6b65\u3002\u5728Python\u4e2d\uff0c\u6587\u4ef6\u8bfb\u53d6\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u7684 <code>open()<\/code> \u51fd\u6570\u5b9e\u73b0\u3002<code>open()<\/code> \u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u6587\u4ef6\u5bf9\u8c61\uff0c\u5b83\u652f\u6301\u8fed\u4ee3\u3001\u8bfb\u53d6\u3001\u5199\u5165\u548c\u5173\u95ed\u7b49\u64cd\u4f5c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;numbers.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = file.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>with open(&#39;numbers.txt&#39;, &#39;r&#39;) as file<\/code> \u8bed\u53e5\u6253\u5f00\u4e86\u4e00\u4e2a\u540d\u4e3a <code>numbers.txt<\/code> \u7684\u6587\u4ef6\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u6587\u4ef6\u5bf9\u8c61 <code>file<\/code>\u3002\u5173\u952e\u5b57 <code>with<\/code> \u786e\u4fdd\u6587\u4ef6\u5728\u4e0d\u518d\u9700\u8981\u65f6\u81ea\u52a8\u5173\u95ed\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p><h3>1.1\u3001\u9010\u884c\u8bfb\u53d6\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u6587\u4ef6\u4e2d\u7684\u6574\u6570\u662f\u9010\u884c\u5b58\u50a8\u7684\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>readlines()<\/code> \u65b9\u6cd5\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;numbers.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    lines = file.readlines()<\/p>\n<p>for line in lines:<\/p>\n<p>    number = int(line.strip())<\/p>\n<p>    print(number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>readlines()<\/code> \u65b9\u6cd5\u8fd4\u56de\u6587\u4ef6\u4e2d\u6240\u6709\u884c\u7684\u5217\u8868\u3002<code>strip()<\/code> \u65b9\u6cd5\u7528\u4e8e\u53bb\u9664\u6bcf\u884c\u672b\u5c3e\u7684\u6362\u884c\u7b26\u548c\u7a7a\u683c\uff0c\u786e\u4fdd <code>int()<\/code> \u8f6c\u6362\u65f6\u4e0d\u4f1a\u51fa\u9519\u3002<\/p>\n<\/p>\n<p><h3>1.2\u3001\u9010\u5b57\u7b26\u8bfb\u53d6\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u6587\u4ef6\u4e2d\u7684\u6574\u6570\u662f\u6309\u5b57\u7b26\u5b58\u50a8\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>read()<\/code> \u65b9\u6cd5\u9010\u5b57\u7b26\u8bfb\u53d6\u6587\u4ef6\uff0c\u7136\u540e\u5206\u5272\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;numbers.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>numbers = content.split()<\/p>\n<p>for number in numbers:<\/p>\n<p>    print(int(number))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>split()<\/code> \u65b9\u6cd5\u6839\u636e\u7a7a\u683c\u5206\u5272\u5b57\u7b26\u4e32\uff0c\u751f\u6210\u4e00\u4e2a\u5305\u542b\u6240\u6709\u6574\u6570\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u6570\u636e\u8f6c\u6362<\/h2>\n<\/p>\n<p><p>\u4ece\u6587\u4ef6\u8bfb\u53d6\u6570\u636e\u540e\uff0c\u901a\u5e38\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u9002\u5f53\u7684\u7c7b\u578b\u3002\u5728\u5904\u7406\u6574\u6570\u65f6\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f <code>int()<\/code> \u51fd\u6570\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6570\u636e\u8f6c\u6362\u524d\u5e94\u786e\u4fdd\u6570\u636e\u7684\u683c\u5f0f\u6b63\u786e\uff0c\u907f\u514d\u8f6c\u6362\u5931\u8d25\u3002<\/p>\n<\/p>\n<p><h3>2.1\u3001\u786e\u4fdd\u6570\u636e\u683c\u5f0f\u6b63\u786e<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6570\u636e\u8f6c\u6362\u524d\uff0c\u786e\u4fdd\u6570\u636e\u683c\u5f0f\u6b63\u786e\u975e\u5e38\u91cd\u8981\u3002\u53ef\u4ee5\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\u6765\u6355\u83b7\u548c\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;numbers.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    lines = file.readlines()<\/p>\n<p>for line in lines:<\/p>\n<p>    try:<\/p>\n<p>        number = int(line.strip())<\/p>\n<p>        print(number)<\/p>\n<p>    except ValueError:<\/p>\n<p>        print(f&quot;Invalid integer: {line.strip()}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>try<\/code> \u5757\u5c1d\u8bd5\u5c06\u6bcf\u884c\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u5982\u679c\u8f6c\u6362\u5931\u8d25\uff0c<code>except<\/code> \u5757\u6355\u83b7 <code>ValueError<\/code> \u5f02\u5e38\u5e76\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u5f02\u5e38\u5904\u7406<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u548c\u6570\u636e\u8f6c\u6362\u65f6\uff0c\u5f02\u5e38\u5904\u7406\u662f\u786e\u4fdd\u7a0b\u5e8f\u7a33\u5065\u6027\u7684\u91cd\u8981\u624b\u6bb5\u3002Python \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u901a\u8fc7 <code>try<\/code>\u3001<code>except<\/code>\u3001<code>finally<\/code> \u7b49\u5173\u952e\u5b57\uff0c\u53ef\u4ee5\u6355\u83b7\u548c\u5904\u7406\u5404\u79cd\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h3>3.1\u3001\u6355\u83b7\u6587\u4ef6\u64cd\u4f5c\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\uff0c\u4f8b\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6743\u9650\u4e0d\u8db3\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528 <code>try<\/code>\u3001<code>except<\/code> \u5173\u952e\u5b57\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    with open(&#39;numbers.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>        data = file.read()<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(&quot;File not found.&quot;)<\/p>\n<p>except PermissionError:<\/p>\n<p>    print(&quot;Permission denied.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>try<\/code> \u5757\u5c1d\u8bd5\u6253\u5f00\u6587\u4ef6\u5e76\u8bfb\u53d6\u6570\u636e\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\u6216\u6743\u9650\u4e0d\u8db3\uff0c<code>except<\/code> \u5757\u6355\u83b7\u76f8\u5e94\u7684\u5f02\u5e38\u5e76\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>3.2\u3001\u6355\u83b7\u6570\u636e\u8f6c\u6362\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u8f6c\u6362\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u683c\u5f0f\u4e0d\u6b63\u786e\u7684\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528 <code>try<\/code>\u3001<code>except<\/code> \u5173\u952e\u5b57\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;numbers.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    lines = file.readlines()<\/p>\n<p>for line in lines:<\/p>\n<p>    try:<\/p>\n<p>        number = int(line.strip())<\/p>\n<p>        print(number)<\/p>\n<p>    except ValueError:<\/p>\n<p>        print(f&quot;Invalid integer: {line.strip()}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>try<\/code> \u5757\u5c1d\u8bd5\u5c06\u6bcf\u884c\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u5982\u679c\u8f6c\u6362\u5931\u8d25\uff0c<code>except<\/code> \u5757\u6355\u83b7 <code>ValueError<\/code> \u5f02\u5e38\u5e76\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u6587\u4ef6\u5173\u95ed<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u6587\u4ef6\u5728\u4e0d\u518d\u9700\u8981\u65f6\u5173\u95ed\u975e\u5e38\u91cd\u8981\u3002\u672a\u5173\u95ed\u7684\u6587\u4ef6\u53ef\u80fd\u5bfc\u81f4\u8d44\u6e90\u6cc4\u6f0f\uff0c\u5f71\u54cd\u7a0b\u5e8f\u6027\u80fd\u548c\u7a33\u5b9a\u6027\u3002Python \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u786e\u4fdd\u6587\u4ef6\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h3>4.1\u3001\u4f7f\u7528 <code>with<\/code> \u5173\u952e\u5b57<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>with<\/code> \u5173\u952e\u5b57\u662f\u786e\u4fdd\u6587\u4ef6\u5728\u4e0d\u518d\u9700\u8981\u65f6\u81ea\u52a8\u5173\u95ed\u7684\u6700\u4f73\u65b9\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;numbers.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = file.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>with<\/code> \u5757\u7ed3\u675f\u65f6\uff0c\u6587\u4ef6\u4f1a\u81ea\u52a8\u5173\u95ed\uff0c\u65e0\u9700\u663e\u5f0f\u8c03\u7528 <code>close()<\/code> \u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>4.2\u3001\u624b\u52a8\u5173\u95ed\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4e0d\u4f7f\u7528 <code>with<\/code> \u5173\u952e\u5b57\uff0c\u53ef\u4ee5\u663e\u5f0f\u8c03\u7528 <code>close()<\/code> \u65b9\u6cd5\u5173\u95ed\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&#39;numbers.txt&#39;, &#39;r&#39;)<\/p>\n<p>data = file.read()<\/p>\n<p>file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u8bfb\u53d6\u6570\u636e\u540e\u663e\u5f0f\u8c03\u7528 <code>file.close()<\/code> \u65b9\u6cd5\u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u7efc\u5408\u793a\u4f8b<\/h2>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u4e2d\u7684\u6574\u6570\u5e76\u8fdb\u884c\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def read_integers_from_file(file_path):<\/p>\n<p>    try:<\/p>\n<p>        with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>            lines = file.readlines()<\/p>\n<p>        integers = []<\/p>\n<p>        for line in lines:<\/p>\n<p>            try:<\/p>\n<p>                number = int(line.strip())<\/p>\n<p>                integers.append(number)<\/p>\n<p>            except ValueError:<\/p>\n<p>                print(f&quot;Invalid integer: {line.strip()}&quot;)<\/p>\n<p>        return integers<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        print(&quot;File not found.&quot;)<\/p>\n<p>    except PermissionError:<\/p>\n<p>        print(&quot;Permission denied.&quot;)<\/p>\n<p>    return []<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>file_path = &#39;numbers.txt&#39;<\/p>\n<p>integers = read_integers_from_file(file_path)<\/p>\n<p>print(f&quot;Read integers: {integers}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>read_integers_from_file<\/code> \u51fd\u6570\u5b9e\u73b0\u4e86\u6587\u4ef6\u8bfb\u53d6\u3001\u6570\u636e\u8f6c\u6362\u3001\u5f02\u5e38\u5904\u7406\u548c\u6587\u4ef6\u5173\u95ed\u7684\u5b8c\u6574\u8fc7\u7a0b\u3002\u51fd\u6570\u8fd4\u56de\u8bfb\u53d6\u7684\u6574\u6570\u5217\u8868\uff0c\u5e76\u5728\u51fa\u73b0\u5f02\u5e38\u65f6\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\u4ece\u6587\u672c\u6587\u4ef6\u8bfb\u53d6\u6574\u6570\u6d89\u53ca\u591a\u4e2a\u6b65\u9aa4\uff0c\u5305\u62ec\u6587\u4ef6\u8bfb\u53d6\u3001\u6570\u636e\u8f6c\u6362\u3001\u5f02\u5e38\u5904\u7406\u548c\u6587\u4ef6\u5173\u95ed\u3002\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684 <code>open()<\/code> \u51fd\u6570\u3001<code>with<\/code> \u5173\u952e\u5b57\u548c\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u53ef\u4ee5\u7a33\u5065\u5730\u5904\u7406\u5404\u79cd\u6587\u4ef6\u548c\u6570\u636e\u5f02\u5e38\u60c5\u51b5\u3002\u5e0c\u671b\u672c\u6587\u8be6\u7ec6\u7684\u4ecb\u7ecd\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5b9e\u73b0\u8fd9\u4e00\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\uff0c\u4f60\u5e94\u8be5\u5df2\u7ecf\u638c\u63e1\u4e86\u5982\u4f55\u5728Python\u4e2d\u4ece\u6587\u672c\u6587\u4ef6\u8bfb\u53d6\u6574\u6570\u7684\u57fa\u672c\u65b9\u6cd5\u548c\u6280\u5de7\u3002\u8fd9\u4e9b\u65b9\u6cd5\u4e0d\u4ec5\u9002\u7528\u4e8e\u5904\u7406\u6574\u6570\uff0c\u8fd8\u53ef\u4ee5\u6269\u5c55\u5230\u5904\u7406\u5176\u4ed6\u7c7b\u578b\u7684\u6570\u636e\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u6587\u4ef6\u64cd\u4f5c\u8fd8\u662f\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u4efb\u52a1\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u548c\u7075\u6d3b\u7684\u673a\u5236\uff0c\u5e2e\u52a9\u4f60\u9ad8\u6548\u5730\u5b8c\u6210\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u4e2d\u7684\u6574\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6765\u6253\u5f00\u6587\u672c\u6587\u4ef6\uff0c\u5e76\u5229\u7528<code>readlines()<\/code>\u6216<code>read()<\/code>\u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u968f\u540e\uff0c\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u5c06\u8bfb\u53d6\u5230\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\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    integers = [int(line.strip()) for line in file if line.strip().isdigit()]\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8bfb\u53d6<code>file.txt<\/code>\u4e2d\u7684\u6bcf\u4e00\u884c\uff0c\u5e76\u5c06\u5176\u4e2d\u7684\u6574\u6570\u5b58\u50a8\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\u3002<\/p>\n<p><strong>\u5904\u7406\u6587\u672c\u6587\u4ef6\u65f6\u5982\u4f55\u786e\u4fdd\u53ea\u63d0\u53d6\u6574\u6570\uff1f<\/strong><br \/>\u5728\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6765\u68c0\u67e5\u6bcf\u884c\u5185\u5bb9\u662f\u5426\u4e3a\u6574\u6570\u3002\u5229\u7528<code>isdigit()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u786e\u5b9a\u5b57\u7b26\u4e32\u662f\u5426\u7531\u6570\u5b57\u7ec4\u6210\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u5757\u53ef\u4ee5\u5904\u7406\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\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    integers = []\n    for line in file:\n        try:\n            number = int(line.strip())\n            integers.append(number)\n        except ValueError:\n            continue  # \u5ffd\u7565\u975e\u6574\u6570\u884c\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u53ea\u63d0\u53d6\u6709\u6548\u7684\u6574\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u8f93\u5165\u7684\u6574\u6570\u5199\u5165\u6587\u672c\u6587\u4ef6\uff1f<\/strong><br \/>\u5c06\u6574\u6570\u5199\u5165\u6587\u672c\u6587\u4ef6\u540c\u6837\u7b80\u5355\u3002\u53ef\u4ee5\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u4ee5\u5199\u5165\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>write()<\/code>\u65b9\u6cd5\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u540e\u5199\u5165\u6587\u4ef6\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]\nwith open(&#39;output.txt&#39;, &#39;w&#39;) as file:\n    for number in numbers:\n        file.write(f&quot;{number}\\n&quot;)\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4f1a\u5c06\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u6574\u6570\u5199\u5165<code>output.txt<\/code>\u6587\u4ef6\uff0c\u6bcf\u4e2a\u6570\u5b57\u5360\u4e00\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u7bb1\u6587\u672c\u6587\u4ef6\u8f93\u5165\u6574\u6570\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5173\u952e\u5728\u4e8e\u9009\u62e9\u5408\u9002\u7684\u6587\u4ef6\u64cd\u4f5c\u65b9\u5f0f\u3001\u786e\u4fdd\u6587\u4ef6\u683c\u5f0f\u6b63\u786e\u3001\u5e76\u6b63\u786e\u5904\u7406 [&hellip;]","protected":false},"author":3,"featured_media":1136868,"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\/1136858"}],"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=1136858"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1136858\/revisions"}],"predecessor-version":[{"id":1136869,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1136858\/revisions\/1136869"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1136868"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1136858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1136858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1136858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}